-1

In this below example, say file.xml There are values inside tags return code=" " I need the values inside <Port name="write_qwe"> ONLY.

<Main display="NORMAL">
    <Port name="read_abc" exe="NO">
        <input>
            <struct file="C:\temp" sign="id1"/>
        </input>
        <output>
            <return code="33" shortmsg="Implementation not found for commande."/>
        </output>
    </Port>
    <Port name="write_qwe" exe="NO">
        <input>
            <struct file="C:\temp" id="id1"/>
        </input>
        <output>
            <return code="1" shortmsg="NOTEXECUTED" longmsg="Not execute due to previous error"/>
        </output>
    </Port>
    <Port name="read_abc" exe="NO">
        <input>
            <struct file="C:\temp" sign="id2"/>
        </input>
        <output>
            <return code="66" shortmsg="Implementation"/>
        </output>
    </Port>
    <Port name="write_qwe" exe="NO">
        <input>
            <struct file="C:\temp" id="id2"/>
        </input>
        <output>
            <return code="0" shortmsg="NOTEXECUTED" />
        </output>
    </Port>
</Main>

I need to get the value of the <return code" "> which is inside <port name="write_*"> and inside <output> . In this example I need to get values "1" and "0" .

maba
  • 47,113
  • 10
  • 108
  • 118
Stella
  • 89
  • 2
  • 5
  • 12

3 Answers3

2

If you dont have any problem in using xpath ,then you can try this :

You have to give the path of your xml file in the argument.

   XPathReader reader = new XPathReader("FileName.xml");

   // To get a xml attribute.
   String expression = "/Main/Port/output/@code";

   System.out.println(reader.read(expression,XPathConstants.STRING) + "n");
vikiiii
  • 9,246
  • 9
  • 49
  • 68
2

XPath is probably the way to go here.

I have put the xml file as a resource but you may have it in a file structure.

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
ClassLoader loader = XmlTestReader.class.getClassLoader();
InputStream is = loader.getResourceAsStream("test.xml");
Document doc = builder.parse(is);

Then create an XPath expression.

XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
XPathExpression expr = xPath.compile("/Main/Port[@name='write_qwe']/output/return/@code");
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

The XPath expression /Main/Port[@name='write_qwe']/output/return/@code will find all code attributes where the Port's attribute name is write_qwe.

And now you can iterate over the nodes like this:

for (int i = 0; i < nl.getLength(); i++) {
    Node node = nl.item(i);
    System.out.println(node.getNodeValue());
}

You can restrict the XPath to /Main/Port[@name='write_qwe']/output/return if you want the whole <return> node instead.

And iterating like this instead:

for (int i = 0; i < nl.getLength(); i++) {
    Node node = nl.item(i);
    System.out.println(node.getAttributes().getNamedItem("code").getNodeValue());
}

Edit

As suggested by the comment by Blaise Doughan it might be better to use an InputSource as input to XPathExpression#evaluate() instead:

ClassLoader loader = XmlTestReader.class.getClassLoader();
InputStream inputStream = loader.getResourceAsStream("test.xml");
InputSource inputSource = new InputSource(inputStream);

XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
XPathExpression expr = xPath.compile("/Main/Port[@name='write_qwe']/output/return/@code");
NodeList nl = (NodeList) expr.evaluate(inputSource , XPathConstants.NODESET);
Community
  • 1
  • 1
maba
  • 47,113
  • 10
  • 108
  • 118
  • +1 - But I would recommend calling the `evaluate` method that takes an `InputSource` instead. This gives the XPath implementation the chance to not build a DOM if it doesn't need one. This can lead to a performance improvement. – bdoughan Aug 29 '12 at 08:44
  • 1
    @BlaiseDoughan I added your suggestion to the answer. Thanks. – maba Aug 29 '12 at 09:11
0

if your XML is a String, Then you can do the following:

String xml = ""; //Populated XML String....
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = DocumentBuilderFactory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(xml)));
Element rootElement = document.getDocumentElement();

If your XML is in a file, then Document document will be instantiated like this:

Document document = builder.parse(new File("file.xml"));

The document.getDocumentElement() returns you the node that is the document element of the document (in your case <config>).

Once you have a rootElement, you can access the element's attribute (by calling rootElement.getAttribute() method), etc. For more methods on java's org.w3c.dom.Element

For further clarification view this link it helps you much...

http://www.java-samples.com/showtutorial.php?tutorialid=152

cheers..!

maba
  • 47,113
  • 10
  • 108
  • 118
Sundar G
  • 1,069
  • 1
  • 11
  • 29