Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

finding a node in XML (java).

Options
  • 03-01-2006 2:50pm
    #1
    Registered Users Posts: 21,264 ✭✭✭✭


    Take this sample xml file..
    <Unit id="test1">
     <Item id="part1 value="12"/>
     <SubItems>
      <Item id="part1 value="xx"/>
     </SubItems>
    
    </Unit>
    

    Now I want to get to "<Item id="part1 value="xx"/>" fast. Is there some kind of search option? Currently the only way I can see to get to this is to work my way though the DOM/Nodelists which seems a bit messy.


Comments

  • Registered Users Posts: 1,982 ✭✭✭lynchie


    Have you tried using xpath? You can use it to find the node you are looking for. If memory serves me right something like "//Item[@id='part1' and value='xx']" should find the node.


  • Closed Accounts Posts: 324 ✭✭madramor


    Hobbes wrote:
    Currently the only way I can see to get to this is to work my way though the DOM/Nodelists which seems a bit messy.

    http://java.sun.com/developer/codesamples/xml.html#sax

    or a hack
    http://www.javaworld.com/javaworld/javatips/jw-javatip128.html?


  • Closed Accounts Posts: 80 ✭✭Torak


    XPath (implemented as part of xalan) can be used on DOM documents.
    org.apache.xpath.XPathAPI.selectNodeList
    
    If performance is an issue then writing a SAX Handler will be considerably better, but other than that the XPath syntax "//Item[@id='part1' and @value='12']" will return all Items with the id attribute with value 'part1' and the value attribute set to 12.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    madramor wrote:

    None of that has anything to do with what I am looking for.

    I'll give the Xalan a bash. cheers.


  • Closed Accounts Posts: 324 ✭✭madramor


    Hobbes wrote:
    Take this sample xml file..
    <Unit id="test1">
     <Item id="part1 value="12"/>
     <SubItems>
      <Item id="part1 value="xx"/>
     </SubItems>
    </Unit>
    
    Now I want to get to "<Item id="part1 value="xx"/>" fast. Is there some kind of search option? Currently the only way I can see to get to this is to work my way though the DOM/Nodelists which seems a bit messy.
    the fastest simpliest way to do what you want is to use a sax parser
    Hobbes wrote:
    None of that has anything to do with what I am looking for.
    how is that?


  • Advertisement
  • Registered Users Posts: 877 ✭✭✭clearz


    I dont know if this is the type of thing you are looking for but it will it iterate through the attributes in all the Item tags. It searches for all the Item tags using the line

    NodeList nodeList = doc.getDocumentElement().getElementsByTagName("Item");
     
     import org.w3c.dom.*;
     import java.io.*;
     import org.xml.sax.*;
     import org.xml.sax.helpers.*;
     import javax.xml.parsers.*;
     
     public class XML
     {
         public static void main(String[] args)
         {
             DocumentBuilder docBuilder;
             Document doc = null;
             String  str = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
                     str += "<Unit id=\"test1\">";
                     str += "<Item id=\"part1\" value=\"12\"/>";
                     str += "<SubItems>";
                     str += "<Item id=\"part1\" value=\"xx\"/>";
                     str += "</SubItems>";
                     str += "</Unit>";
     
             DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
             docBuilderFactory.setIgnoringElementContentWhitespace(true);
     
             try
             {
                 docBuilder = docBuilderFactory.newDocumentBuilder();
                  doc = docBuilder.parse(new InputSource(new StringReader(str)));
             }
             catch (Exception e)
             {
                 System.out.println(e.getMessage());
             }
     
              NodeList nodeList = doc.getDocumentElement().getElementsByTagName("Item");
              for(int i=0;i<nodeList.getLength();i++)
              {
                 Node n1 = nodeList.item(i);
                 NamedNodeMap nnm = n1.getAttributes();
                  System.out.println(n1.getNodeName());
     
                 for(int t=0;t<nnm.getLength();t++)
                      System.out.println("\t" + nnm.item(t).getNodeName() + "=" + nnm.item(t).getNodeValue());
             }
         }
     }
    

    using DOM always gets messy depending on the complexity of your XML document. There are other 3rd party packages available which are designed to make parsing XML documents easier but I havent tried any. You could start by looking at JDOM.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    madramor wrote:
    the fastest simpliest way to do what you want is to use a sax parser

    Well for what I am doing it isn't really. I am creating the dom to begin with and it is still in the process of being created when I need to look up information. Sax doesn't appear to be able to cater for this. Thanks anyway.

    ... anyway after a lot of pulling my hair out I gave up and just coding to do the searching/updating my own way and then when the data is compiled send it out to a dom object for writing. Seems quickest way.


Advertisement