Getting Xml Node Text Value with Java Dom

Getting XML Node text value with Java DOM

I'd print out the result of an2.getNodeName() as well for debugging purposes. My guess is that your tree crawling code isn't crawling to the nodes that you think it is. That suspicion is enhanced by the lack of checking for node names in your code.

Other than that, the javadoc for Node defines "getNodeValue()" to return null for Nodes of type Element. Therefore, you really should be using getTextContent(). I'm not sure why that wouldn't give you the text that you want.

Perhaps iterate the children of your tag node and see what types are there?

Tried this code and it works for me:

String xml = "<add job=\"351\">\n" +
" <tag>foobar</tag>\n" +
" <tag>foobar2</tag>\n" +
"</add>";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
ByteArrayInputStream bis = new ByteArrayInputStream(xml.getBytes());
Document doc = db.parse(bis);
Node n = doc.getFirstChild();
NodeList nl = n.getChildNodes();
Node an,an2;

for (int i=0; i < nl.getLength(); i++) {
an = nl.item(i);
if(an.getNodeType()==Node.ELEMENT_NODE) {
NodeList nl2 = an.getChildNodes();

for(int i2=0; i2<nl2.getLength(); i2++) {
an2 = nl2.item(i2);
// DEBUG PRINTS
System.out.println(an2.getNodeName() + ": type (" + an2.getNodeType() + "):");
if(an2.hasChildNodes()) System.out.println(an2.getFirstChild().getTextContent());
if(an2.hasChildNodes()) System.out.println(an2.getFirstChild().getNodeValue());
System.out.println(an2.getTextContent());
System.out.println(an2.getNodeValue());
}
}
}

Output was:

#text: type (3): foobar foobar
#text: type (3): foobar2 foobar2

How to get an XML node values using Java?

Well, the xml file don't have this node.

  doc.getElementsByTagName("staff");

Java XML Node Inside Node Value

For an Element is defined the method getAttribute("AttributeName").

If you want the name of the cars inside a garage you are doing right by creating a NodeList within all the elements by tag name "garage", then you should create a nested for loop, to get all the elements by tag name "cars" inside your garage.

    for (int temp = 0; temp < NodeOzet.getLength(); temp++){
Node nOzet = NodeOzet.item(temp);
NodeList carList = eElement.getElementsByTagName("cars")
String cars = "";
for(int temp2 = 0; temp2 < carList.getLength(); temp2++){
cars += ((Element)carList.item(temp2)).getAttribute("carname") + "\t";
}
}

Like this for each garage you get all the cars inside the garage, and their names

Get a node's inner XML as String in Java DOM

Same problem. To solve it I wrote this helper function:

public String innerXml(Node node) {
DOMImplementationLS lsImpl = (DOMImplementationLS)node.getOwnerDocument().getImplementation().getFeature("LS", "3.0");
LSSerializer lsSerializer = lsImpl.createLSSerializer();
NodeList childNodes = node.getChildNodes();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < childNodes.getLength(); i++) {
sb.append(lsSerializer.writeToString(childNodes.item(i)));
}
return sb.toString();
}

How can i get element text in node in Java?

Finally, i get this works.

I use info.getFirstChild().getNodeValue()

        for (int y = 0; y < ClinicInfo.getLength(); y++) {
Node info = ClinicInfo.item(y);
if (info.hasChildNodes()) {
Log.e(info.getNodeName(), info.getFirstChild().getNodeValue());
}
}

Line 3 is important, i Log the hasChildNodes() and watch in LogCat, don't know why it contains a node named "#text" and no childnodes (which is false on logcat).

But i believe my XML is correct format.
After Google, found many explanation.
https://www.google.com/search?q=xml+%23text

Sample Image

Getting text value from java DOM

You appear to be wanting the values which are siblings to the param-name nodes.

  1. Use the equals method to check object equality (not ==)
  2. Whitespace creates text nodes

Consider using XPath:

  public static void printNode(Document d) {
try {
NodeList values = (NodeList) XPathFactory.newInstance()
.newXPath()
.evaluate("//param-value/text()", d, XPathConstants.NODESET);

for (int i = 0; i < values.getLength(); i++) {
System.out.println(values.item(i).getTextContent());
}
} catch (XPathExpressionException e) {
throw new IllegalStateException(e);
}
}

Java XML - Node Name and Value retrieval not as expected

It seems the first question you should consider is this:

What is the Document Object Model (DOM) and how does it relate to my findings in below?

That answer is found in the formal specification of the DOM

Paraphrasing that reference, one finds:

the nodes in the [DOM] do not represent a data structure, they
represent objects, which have functions and identity

We also find a glossary entry for element:

Each document contains one or more elements, the boundaries of
which are either delimited by start-tags and end-tags, or, for empty
elements by an empty-element tag. Each element has a type, identified
by name, and may have a set of attributes. Each attribute has a name
and a value. See Logical Structures in XML [XML 1.0].

The term Document Object Model suggests the DOM is object oriented. As such, it turns out, a Document is a Node that has Elements that are also Nodes.

With this background, we can see that the actual DOM manipulation takes place upon the API provided by the Node interface and the actual structure the XML tags can be discovered using the Element interface.

Now your questions are easy to answer:

Question 1 : What determines whether to use an Element or Node?

  1. if you want to manipulate the DOM, use a Node
  2. if you want to get attributes from tags use an Element
  3. Since Element is a Node you can just use Element for everything

Question 2 : Why didn't getFirstChild() return Location as expected?

The first child of that Node is a TextNode - Every Node can potentially have text between the opening/closing tags of its Element. So, each Node has a child node that is a Text Node ref1, ref2

Question 3 : Why was it required to call both getFirstChild() and getNextSibling() in order to see the first child?

it isn't. All you did is traverse the DOM to get to the Node you expected to be the first Node - but as you now know, it wasn't.

How to remove #text from my Node parsing in Java dom xml parsing

With DTD validation you can have the parser automatically suppress the whitespace between elements. However to modify your specific implementation, you can test for Text nodes, and ignore them if they are empty.

private void listNodes(Node node, String indent)
{
if (node instanceof Text) {
String value = node.getNodeValue().trim();
if (value.equals("") ) {
return;
}
}

String nodeName = node.getNodeName();
System.out.println(indent + " Node is: " + nodeName);
...


Related Topics



Leave a reply



Submit