Results 1 to 12 of 12
- 01-18-2012, 07:06 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 57
- Rep Power
- 0
Showing only the child nodes for a selected node
Hi
Im currently trying to display the information from a xml file that looks like this:(I know its all listed in attributes)
Im using DOM to parse the file and get all the info out in the console, showing first the Type attribute then the Item attribute. I would like to be able to select only the type small and list out what items are listed under that node.Java Code:<Component> <Type Name="Small"> <Item ItemNo="1" Name="Sweater" Value="4" Config="1"></Item> <Item ItemNo="3" Name="Shoes" Value="5" Config="2"></Item> </Type> <Type Name="Large"> <Item ItemNo="6" Name="Pants" Value="5" Config="1"></Item> <Item ItemNo="9" Name="Shirt" Value="3" Config="1"></Item> </Type> </Component>
can some one help me.
I have used DOM parser example and adapted it to my use.
Here is how I get the node attributes:
And to get the listsJava Code:private void parseDocument(){ //get the root elememt Element docEle = dom.getDocumentElement(); //get a nodelist of <employee> elements NodeList nl = docEle.getElementsByTagName("Item"); if(nl != null && nl.getLength() > 0) { for(int i = 0 ; i < nl.getLength();i++) { //get the employee element Element el = (Element)nl.item(i); //get the Employee object Item e = getItem(el); //add it to list myEmpls.add(e); } } NodeList nlT = docEle.getElementsByTagName("Type"); if(nlT != null && nlT.getLength() > 0) { for(int i = 0 ; i < nlT.getLength();i++) { //get the employee element Element elT = (Element)nlT.item(i); //get the Employee object Type eT = getType(elT); //add it to list MyTypes.add(eT); } } }
Hoping someone can help meJava Code:private Item getItem(Element empEl) { String ItemNo = empEl.getAttribute("ItemNo"); String Name = empEl.getAttribute("Name"); String Value = empEl.getAttribute("Value"); String Config = empEl.getAttribute("Config"); Item e = new Item(ItemNo,Name, Value, Config); return e; } private Type getType(Element empEl) { String Name = empEl.getAttribute("Name"); Type eT = new Type(Name); return eT; }
- 01-18-2012, 11:29 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Showing only the child nodes for a selected node
Since you're using the DOM, and therefore building a document, you ought to be using XPath to get the nodes you want.
- 01-18-2012, 11:58 AM #3
Member
- Join Date
- Dec 2011
- Posts
- 57
- Rep Power
- 0
Re: Showing only the child nodes for a selected node
Ok. I read somewhere that the DOM is the way to go if you are going to remove and add nodes (as Im going to implement later on. One step at the time;) )
A quick search gave me som info ont he XPath. like this tutorial.
Lars Vogel also have some interesting reading.
I also see that I have to do some double parsing (?) in order to achieve what I want
Have to read up on it.
Thanks for replyLast edited by KarlNorway; 01-18-2012 at 12:02 PM. Reason: Adding link
- 01-18-2012, 12:14 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Showing only the child nodes for a selected node
w3schools is also usually a good start point for the basics.
Of course there's also the horses mouth.
- 01-19-2012, 07:14 AM #5
Member
- Join Date
- Dec 2011
- Posts
- 57
- Rep Power
- 0
Re: Showing only the child nodes for a selected node
So If I get things right here I query the xml file using XPath and use DOM (or similar) when I want to edit the file. ie. add or delete nodes.
- 01-24-2012, 01:55 PM #6
Member
- Join Date
- Dec 2011
- Posts
- 57
- Rep Power
- 0
Re: Showing only the child nodes for a selected node
Here I go again.
I would so like to use DOM (since Im sort of getting it). Looking in to Treewalker to se if can't get hold of the childNodes
This is one way I have found. And there are several more tutorials to read from..
- 02-07-2012, 07:09 AM #7
Member
- Join Date
- Dec 2011
- Posts
- 57
- Rep Power
- 0
Re: Showing only the child nodes for a selected node
Ok So I have gotten the dom parser to stop at given type name node.
Just not yet been able to pars only the child nodes.
Here is the code (Bugtracking with system out println)
Anyone that can help me? Or have a code example (or link to example) with other method of doing this?Java Code:void sortTypes() { Element docEle = dom.getDocumentElement(); // System.out.println("sortTypes enterd"); NodeList nlT = docEle.getElementsByTagName("Type"); // System.out.println("Types found \n searching for:" + "[" + Selected + "]"); if(nlT != null && nlT.getLength() > 0) { for(int i = 0 ; i < nlT.getLength();i++) { Element elT = (Element)nlT.item(i); // //get the Employee object Type eT = getType(elT); // System.out.println("Processing \n" + eT); String stop = String.valueOf(eT); if(stop.equals(Selected)){ // parseDocument(); // Element elTT = (Element) docEle.getElementsByTagName(Selected); NodeList list = elT.getChildNodes(); if (list.getLength() > 0) { System.out.println("Child Nodes of " + eT + " are: "); System.out.println("Type Name: " + elT.getAttribute("Name")); for (int k = 0; k < list.getLength(); k++){ System.out.println(list); System.out.println(k); // } } break; } } } }
- 02-08-2012, 07:11 AM #8
Member
- Join Date
- Dec 2011
- Posts
- 57
- Rep Power
- 0
Re: Showing only the child nodes for a selected node
Using this Xpath code:
I get this output:Java Code:void sortTypes()throws ParserConfigurationException, SAXException, IOException, XPathExpressionException { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); DocumentBuilder builder; builder = domFactory.newDocumentBuilder(); Document doc = builder.parse("CNodeComponents.xml"); XPath xpath = XPathFactory.newInstance().newXPath(); XPathExpression ItemNo = xpath.compile("//Type[@Name='"+Selected+"']/Item[@ItemNo]"); XPathExpression ItemName = xpath.compile("//Type[@Name='"+Selected+"']/Item[@Name]"); XPathExpression Value = xpath.compile("//Type[@Name='"+Selected+"']/Item[@Value]"); XPathExpression Config = xpath.compile("//Type[@Name='"+Selected+"']/Item[@Config]"); Object itemresult = ItemNo.evaluate(doc, XPathConstants.NODESET); NodeList itemnodes = (NodeList) itemresult; for (int i = 0; i < itemnodes.getLength(); i++) { System.out.println(itemnodes.item(i).getAttributes()); }
Anyone got any idea on how to print the actual attributes? and not just memory space ;)Java Code:[Battery] org.apache.xerces.dom.AttributeMap@1dff3a2 org.apache.xerces.dom.AttributeMap@1f42b49 org.apache.xerces.dom.AttributeMap@145e044 org.apache.xerces.dom.AttributeMap@86c347
- 02-08-2012, 11:06 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Showing only the child nodes for a selected node
Write a method to print out the attributes you want to see of the AttributeMap.
Unless you can find a handy print() method in Xerces for that class (I wouldn't bet on it).
- 02-08-2012, 02:04 PM #10
Member
- Join Date
- Dec 2011
- Posts
- 57
- Rep Power
- 0
Re: Showing only the child nodes for a selected node
Some thing like this :
And then just split it like I already doJava Code:String ItemNo = listItem.getAttribute("ItemNo"); String Name = listItem.getAttribute("Name"); String Value = listItem.getAttribute("Value"); String Config = listItem.getAttribute("Config"); String [] Items = {ItemNo, Name, Value, Config};
- 02-08-2012, 02:19 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Showing only the child nodes for a selected node
If it gives you what you want.
- 02-08-2012, 02:43 PM #12
Member
- Join Date
- Dec 2011
- Posts
- 57
- Rep Power
- 0
Similar Threads
-
Getting Child Nodes With XPath.evaluate on an Element as Opposed to a Node
By baiguai in forum XMLReplies: 5Last Post: 12-21-2011, 10:45 PM -
How to get complete child nodes of an xml
By poorni in forum XMLReplies: 1Last Post: 07-14-2010, 08:58 AM -
Get selected Node Value of a child element
By Ms.Ranjan in forum XMLReplies: 6Last Post: 10-06-2009, 09:31 PM -
How to add different menuitems to child node
By sudhakar.cheru in forum AWT / SwingReplies: 2Last Post: 09-16-2009, 06:32 AM -
How to get value of specific child node
By sito42 in forum New To JavaReplies: 1Last Post: 07-13-2009, 12:00 PM


LinkBack URL
About LinkBacks
Reply With Quote
)

Bookmarks