On Oct 1, 12:17 am, 12ock <[email protected]> wrote: > Hi, > > I am writing a generic xml reader using XMLParser, below is a sample > xml file i am trying to parse: > > <Employee-Detail> > <Employee> > <Emp_Id>E-001</Emp_Id> > </Employee> > </Employee-Detail> > > And here is the code: > > public void onSuccess(String xml_) { > Document doc_ = XMLParser.parse(xml_); > > if (doc_.hasChildNodes()) { > NodeList parentNodes = > doc_.getChildNodes(); > for (int i=0; > i<parentNodes.getLength(); i++) { > TreeItem parent = > tree.addItem(parentNodes.item(i).getNodeName()); > parseXml(parentNodes.item(i), > parent); > } > } > } > > private void parseXml(Node node_, TreeItem parent_) { > if (node_.hasChildNodes()) { > NodeList list_ = node_.getChildNodes(); > System.out.println(list_.getLength()); > for (int i=0; i<list_.getLength(); i++) { > > System.out.println(list_.item(i).getNodeName()); > TreeItem child_ = > parent_.addItem(list_.item(i).getNodeName()); > parseXml(list_.item(i), child_); > } > } else { > parent_.addItem("Value: " + node_.getNodeValue()); > } > } > > What is happening is that when iterate to the last level, which is > Emp_id, node_.hasChildNodes() still returns true and returns #test as > its child node. I omitted all carriage or white spaces. > > Please help!
That's just how the DOM works: in <Emp_Id>E-001</Emp_Id> you have two nodes: an Element node (Emp_Id) and a child Text node. You'd want to check node_.getNodeType() == Node.ELEMENT_NODE rather than (or in addition to) node_.hasChildNodes(). http://www.w3.org/TR/DOM-Level-3-Core/core.html -- You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
