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)
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>
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.
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:
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);
}
}
}
And to get the lists
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;
}
Hoping someone can help me
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.
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 reply
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.
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.
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..
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)
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;
}
}
}
}
Anyone that can help me? Or have a code example (or link to example) with other method of doing this?
Re: Showing only the child nodes for a selected node
Using this Xpath code:
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());
}
I get this output:
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
Anyone got any idea on how to print the actual attributes? and not just memory space ;)
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).
Re: Showing only the child nodes for a selected node
Some thing like this :
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};
And then just split it like I already do
Re: Showing only the child nodes for a selected node
If it gives you what you want.
Re: Showing only the child nodes for a selected node
It almost does. Slight change and I get the desired result. (yay for me :D: )