Java & xml Problem

B

Brian Terry

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
 
T

Tjerk Wolterink

Brian said:
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


So you want us to give you the code??

Maybe you could try it yourself first.
 
B

Brian Terry

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());
}
}

}

}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top