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="priyanka.chaddha@gmail.com" cc="richard.gayle@gmail.com"main="richard.gayle@gmail.com"/>
        <text mnofield="000020"/>
    </errors>
</abc>
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
shefali
  • 19
  • 3
  • 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... – Martin Frank Jun 06 '16 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? – Arjan Jun 06 '16 at 13:40

1 Answers1

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

ma3stro
  • 313
  • 2
  • 11