1

Trying to get the text from "value" attribute. How do I do that using Java DOM? I will later have to go through thousands of *.xml files, that are 20 times this big and look for "failing_message" and then need to get its failing_message, that is in "value" attribute.

<?xml version="1.0" encoding="UTF-8"?>
<root version="14" libraryDocVersion="17">
  <node type="flow" id="3224122a-b164-422c-add2-974f22229b6a">
    <child name="inputs">
      <collection type="list">
        <node type="staticBinding" id="3333333-9dd4-4363-9f15-1333c433335">
                <attribute name="annotation"></attribute>
                <attribute name="assignFromContext">false</attribute>
                <attribute name="assignToContext">false</attribute>
                <attribute name="inputSymbol">failing_message</attribute>
                <attribute name="inputType">String</attribute>
                <attribute name="isList">false</attribute>
                <attribute name="isPersisted">true</attribute>
                <attribute name="last_modified_by">admin</attribute>
                <attribute name="listDelimiter">,</attribute>
                <attribute name="modifiedTimestamp">1428938670220</attribute>
                <attribute name="record">false</attribute>
                <attribute name="required">true</attribute>
                <attribute name="uuid">3333333-30c4-3333-3333-333800d10333</attribute>
                <attribute name="value">Could not get free IP address for installation</attribute>
         </node>
       </collection>
    </child>
  </node>
</root>
schlumpfpirat
  • 195
  • 2
  • 12
  • possible duplicate of [Getting XML Node text value with Java DOM](http://stackoverflow.com/questions/773012/getting-xml-node-text-value-with-java-dom) – mateuscb Jul 01 '15 at 14:28

2 Answers2

1

You should use xpath to search XML documents. it is the most efficient way to search a loaded XML doc.

Here is the piece of code to get you the text value of an attribute element with a name="value" attribute

import javax.xml.parsers.*;
import javax.xml.xpath.*;
import org.w3c.dom.*;

public static void main(String[] args)
{
    try {
            Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("C://Temp/xx.xml");

            XPath xPath =  XPathFactory.newInstance().newXPath();
            Node n = (Node)xPath.compile("//attribute[@name='value']")
                    .evaluate(doc, XPathConstants.NODE);
            System.out.println(n.getTextContent());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Output

Could not get free IP address for installation
Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47
0

for you simple usecase you can use sax, because it is faster:

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParserFactory;

public class ValueHandler extends DefaultHandler{

    private boolean valueAttributeElement;
    private StringBuilder content = new StringBuilder();
    private String answer;

    @Override
    public void startElement(String uri, String localName, String qname, Attributes attributes) throws SAXException{
        valueAttributeElement = localName.equals("attribute") && "value".equals(attributes.getValue("name"));
        content.setLength(0);
    }

    @Override
    public void characters(char[] chars, int start, int length) throws SAXException{
        if(valueAttributeElement)
            content.append(chars, start, length);
    }

    @Override
    public void endElement(String uri, String localName, String qname) throws SAXException{
        if(valueAttributeElement)
            answer = content.toString();
    }

    public String getAnswer(){
        return answer;
    }

    public static void main(String[] args) throws Exception{
        String file = "/Users/santhosh/tmp/jlibs-test/src/main/resources/test.xml";
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        ValueHandler valueHandler = new ValueHandler();
        factory.newSAXParser().parse(file, valueHandler);
        System.out.println("answer: "+valueHandler.getAnswer());
    }
}

output is:

answer: Could not get free IP address for installation
Santhosh Kumar Tekuri
  • 3,012
  • 22
  • 22