Monday 15 February 2010

xml - Using Document Nodes in Java -


I am trying to read an XML file, for example:

I should be enabled & lt; Item & gt; getTextContent () for each title, description, etc. for the tag a & lt; Title & gt; tag is one such & lt; Item & gt; is not a child with the tag that I do not want getTextContent () .

I have set up my XML reader so that I have:

  document doc = dbuilder.parse (xmlFile); Doc.getDocumentElement () Normal () .; String title = document.getElementsByTagName ("title"). Items (0) .getTextContent ();   

But this method & lt; Title & gt; which is & lt; Item & gt;

I can simply change the object (0) to object (1) , but I need this algorithm so that the different Can be worked with XML files which are not the initial and lieutenant; without gtc: heading

I'll call those & lt; Title & gt; & lt; Item & gt; s

Instead of using XPath makes it all very easy:

  XPathFactory xpf = XPathFactory.newInstance (); XPath xp = xpf.newXPath (); Node list nl = (nodelist) xp.evaluate ("// items / title / text ()", doctor, XPathConstants.NODESET); For (int i = 0; i & lt; nl.getLength (); ++ i) {System.out.println (nl.item (i) .getNodeValue ()); }    

No comments:

Post a Comment