Results 1 to 12 of 12
  1. #1
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default 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)
    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>
    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:

    Java 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
    Java 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

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default 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.

  3. #3
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default 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
    Last edited by KarlNorway; 01-18-2012 at 01:02 PM. Reason: Adding link

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default 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.

  5. #5
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default 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.

  6. #6
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default 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..

  7. #7
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default 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)

    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;
            	 }
    				}
    			}
        }
    Anyone that can help me? Or have a code example (or link to example) with other method of doing this?

  8. #8
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default Re: Showing only the child nodes for a selected node

    Using this Xpath code:
    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()); 
    					  }
    I get this output:
    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
    Anyone got any idea on how to print the actual attributes? and not just memory space ;)

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default 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).

  10. #10
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default Re: Showing only the child nodes for a selected node

    Some thing like this :

    Java 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

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Showing only the child nodes for a selected node

    If it gives you what you want.

  12. #12
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default Re: Showing only the child nodes for a selected node

    It almost does. Slight change and I get the desired result. (yay for me )

Similar Threads

  1. Replies: 5
    Last Post: 12-21-2011, 11:45 PM
  2. Replies: 1
    Last Post: 07-14-2010, 09:58 AM
  3. Get selected Node Value of a child element
    By Ms.Ranjan in forum XML
    Replies: 6
    Last Post: 10-06-2009, 10:31 PM
  4. How to add different menuitems to child node
    By sudhakar.cheru in forum AWT / Swing
    Replies: 2
    Last Post: 09-16-2009, 07:32 AM
  5. How to get value of specific child node
    By sito42 in forum New To Java
    Replies: 1
    Last Post: 07-13-2009, 01:00 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •