0

I have the following xml.I need to get the value of bcc,cc,main,subject from the above xml.

How can I parse this?

<?xml version="1.0" encoding="UTF-8"?>
<abc>
    <pqr ref1="8340403366" ref2="0000000228754072" ref3="3200051014">
    </pqr>
    <errors>
        <subject>Error is there</subject> 
        <mailto bcc="[email protected]" cc="[email protected]"main="[email protected]"/>
        <text mnofield="000020"/>
    </errors>
</abc>
2
  • 1
    have you tried to read it using DOM or XPath? if not, go to your computer and try it... it will work, i'm pretty sure it will... Commented Jun 6, 2016 at 13:10
  • The xml is malformatted, there should be a space before main= and /> in the mailto element. Are your xml files really like that, or are they just typos in the question? Commented Jun 6, 2016 at 13:40

1 Answer 1

0

If you are trying to parse an xml file something like this could help you out:

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

Document doc = dBuilder.parse(file);
XPath xPath = XPathFactory.newInstance().newXPath();

String expression = "//pqr/@ref1";  // This line defines the element or attribute that you want to retrieve
Node node = (Node) xPath.compile(expression).evaluate(doc, XPathConstants.NODE);
String nodeValue = node.getTextContent();

If your xml file has repeating elements and attributes you might want to store your values in a NodeList. For that you could use:

NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET);

You should check out these links:

http://www.roseindia.net/tutorials/xPath/index.shtml

https://msdn.microsoft.com/en-us/library/ms256086(v=vs.110).aspx

Sign up to request clarification or add additional context in comments.

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.