1

I have an xml trying to parse & read it, but dont know how many nodes the xml may contain? So I am trying to read the node & node values ?

How I get the same say:

<company>
    <personNam>John</personName>
    <emailId>[email protected]</emaiId>
    <department>Products</department>
    (may have additionaly nodes & values for same)
</company>

Sorry forgot to add my code, using Dom:-

Document document = getDocumentBuilder().parse(new ByteArrayInputStream(myXML.getBytes("UTF-8")));          
String xPathExp = "//company";
XPath xPath = getXPath();
NodeList nodeList = (NodeList)xPath.evaluate(xPathExp, document, XPathConstants.NODESET);           
nodeListSize = nodeList.getLength();
System.out.println("#####nodeListSize"+nodeListSize);

for(int i=0;i<nodeListSize;i++){
    element=(Element)nodeList.item(i);
    m1XMLOutputResponse=element.getTextContent();
    System.out.println("#####"+element.getTagName()+"   "+element.getTextContent());
}
3
  • Are you using a SAX parser or a DOM parser? Could you post the code you have written? Commented Apr 10, 2011 at 9:11
  • What parser are you using, and what are you trying to do with the data? Can you post the code you've got so far? Commented Apr 10, 2011 at 9:11
  • Dupe of stackoverflow.com/questions/4076910/… Commented Aug 1, 2013 at 15:54

2 Answers 2

1

Consider using the JAXB library. It's really a painless way of mapping your XML to Java classes and back. The basic principle is that JAXB takes your XML Schemas (XSD) and generates corresponding Java classes for you. Then you just call marshall or unmarshall methods which populate your Java class with the contents of the XML, or generates the XML from your Java class.

The only drawback is, of course, that you'd need to know how to write the XML Schemas :)

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

1 Comment

sorry still not able to get it. for example get personNam : John & .. so on any one having the code
0

Learn how to use XML DOM. Here is an example on how to use XML DOM to fetch node and node values.

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.