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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

finding a node in XML (java).

  • 03-01-2006 1:50pm
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 Posts: 2,013 ✭✭✭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, Registered Users 2 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, Registered Users 2 Posts: 885 ✭✭✭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, Registered Users 2 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