Saturday, 4 April 2015

Cannot print the output of the given node. The root Element can only be printed



I'm just starting to learn JAVA xml = DOM4J which is based on TutorialsPoint is much more flexible of parsing a xml code. So I focused on understanding more the DOM4J because of it's less coding compared to DOM parser and JDOM.


I have a problem in printing the output of the Node. Any help would appreciate! Please bear with me since I am still learning how to use it and be more familiarize with it. For more understanding kindly explain in an easy how to execute it properly. Thank you!


My code based from TutorialsPoint



try {
String src = "student.xml";
File inputFile = new File(src);
SAXReader reader = new SAXReader();

Document doc = reader.read(inputFile);

System.out.println("Root element: "
+ doc.getRootElement().getName());

Element classElement = doc.getRootElement();

List<Node> nodes = doc.selectNodes("/Section/Student");

for (Node node : nodes){
System.out.println("First Name" + node.selectSingleNode("FirstName").getText());
}

} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


Here is my XML file



<?xml version = "1.0"?>
<Section>

<Student>
<Idnumber>0000001</Idnumber>
<FirstName>Cat</FirstName>
<LastName>Dog</LastName>
</Student>
</Section>


The errors I have receiving in the console are the following:


Root element :Section Exception in thread "main" java.lang.NoClassDefFoundError:org/jaxen/JaxenException at org.dom4j.DocumentFactory.createXPath(DocumentFactory.java:230) at org.dom4j.tree.AbstractNode.createXPath(AbstractNode.java:207) at org.dom4j.tree.AbstractNode.selectNodes(AbstractNode.java:164) at com.dls.csb.ParsingXML.main(ParsingXML.java:30)


No comments:

Post a Comment