Java & xml Problem

Discussion in 'XML' started by Brian Terry, Mar 7, 2005.

  1. Brian Terry

    Brian Terry Guest

    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!
    Brian Terry, Mar 7, 2005
    #1
    1. Advertising

  2. Brian Terry wrote:
    > 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!



    So you want us to give you the code??

    Maybe you could try it yourself first.
    Tjerk Wolterink, Mar 7, 2005
    #2
    1. Advertising

  3. Brian Terry

    Brian Terry Guest

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

    import org.dom4j.Document;
    import org.dom4j.Node;
    import org.dom4j.io.DOMReader;

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import java.util.Iterator;
    import java.util.List;

    public class TestSearch {

    public static void main(String[] args)
    throws Exception {
    if (args.length < 2) {
    System.out.println("usage: TestSearch file value");
    System.exit(0);
    }
    // parse a DOM tree
    DocumentBuilderFactory factory =
    DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    org.w3c.dom.Document domDocument = builder.parse(args[0]);

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

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

    }

    }





    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Brian Terry, Mar 8, 2005
    #3
  4. Make sure to include dom4j on the classpath.

    Regards,
    Edwin
    Edwin Dankert, Mar 13, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bomb Diggy
    Replies:
    0
    Views:
    431
    Bomb Diggy
    Jul 28, 2004
  2. Tony Prichard
    Replies:
    0
    Views:
    710
    Tony Prichard
    Dec 12, 2003
  3. Mark Van Orman

    embedding xml in xml as non-xml :)

    Mark Van Orman, Sep 14, 2004, in forum: XML
    Replies:
    5
    Views:
    464
    Patrick TJ McPhee
    Sep 15, 2004
  4. jkflens
    Replies:
    2
    Views:
    1,444
    jkflens
    May 30, 2006
  5. Erik Wasser
    Replies:
    5
    Views:
    428
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page