Java & xml Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Terry

    Java & xml Problem

    I am new in Java. And asking for help.
    The problem is to parse the XML doc and search the parse tree for the
    value and then print path to it.
    For instance:
    XML document:
    <1>
    <2>
    <3>value1</3>
    <4>value2</4>
    </2>
    </1>
    c:\ java XMLParser doc.xml value1
    path is: 1 -> 2 -> 3

    c:\ java XMLParser doc.xml value2
    The path is: 1 -> 2 -> 4
    Have any ideas?
    Thanks for attention


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • Tjerk Wolterink

    #2
    Re: Java &amp; xml Problem

    Brian Terry wrote:[color=blue]
    > I am new in Java. And asking for help.
    > The problem is to parse the XML doc and search the parse tree for the
    > value and then print path to it.
    > For instance:
    > XML document:
    > <1>
    > <2>
    > <3>value1</3>
    > <4>value2</4>
    > </2>
    > </1>
    > c:\ java XMLParser doc.xml value1
    > path is: 1 -> 2 -> 3
    >
    > c:\ java XMLParser doc.xml value2
    > The path is: 1 -> 2 -> 4
    > Have any ideas?
    > Thanks for attention
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it![/color]


    So you want us to give you the code??

    Maybe you could try it yourself first.

    Comment

    • Brian Terry

      #3
      Re: Java &amp; xml Problem

      Ok,
      i am using dom4j. And have such error:
      'Exception in thread "main" java.lang.NoCla ssDefFoundError :
      org/dom4j/io/DOMReader'. Can u help me to solve it? The code is here:

      import org.dom4j.Docum ent;
      import org.dom4j.Node;
      import org.dom4j.io.DO MReader;

      import javax.xml.parse rs.DocumentBuil der;
      import javax.xml.parse rs.DocumentBuil derFactory;
      import java.util.Itera tor;
      import java.util.List;

      public class TestSearch {

      public static void main(String[] args)
      throws Exception {
      if (args.length < 2) {
      System.out.prin tln("usage: TestSearch file value");
      System.exit(0);
      }
      // parse a DOM tree
      DocumentBuilder Factory factory =
      DocumentBuilder Factory.newInst ance();
      DocumentBuilder builder = factory.newDocu mentBuilder();
      org.w3c.dom.Doc ument domDocument = builder.parse(a rgs[0]);

      // Now convert to DOM4J model
      DOMReader reader = new DOMReader();
      Document document = reader.read(dom Document);

      // List nodes with search value
      List nodes = document.select Nodes("//*[starts-with(., '" + args[1] +
      "')]");
      for (Iterator it = nodes.iterator( ); it.hasNext();) {
      Node node = (Node) it.next();
      if (node.getText() .equals(args[1])) {
      System.out.prin tln(node.getPat h());
      }
      }

      }

      }





      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Edwin Dankert

        #4
        Re: Java &amp; xml Problem

        Make sure to include dom4j on the classpath.

        Regards,
        Edwin

        Comment

        Working...