-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" .

5
  • 1
    I'd suggest to use DOM,XPath Commented Aug 29, 2012 at 6:59
  • try this it will help you.............. stackoverflow.com/questions/773012/… Commented Aug 29, 2012 at 7:01
  • @paul I have done the code till, wheather it checks "read_abc" or "write_qwe" Commented Aug 29, 2012 at 7:05
  • @Sath : yes, I checked,but it doesnot give the values inside a tag (return code=" ") Commented Aug 29, 2012 at 7:07
  • And what difficulty are you having performing the check? Commented Aug 29, 2012 at 7:10

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

+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.
@BlaiseDoughan I added your suggestion to the answer. Thanks.
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");

Comments

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..!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.