Hello,
I'm trying to do a program that receive an xml file and ought to create all the neccesary java objects according to the content of the parsed xml file.
I've all the class created for all the objects that could be present into the xml and the idea is to go down in the tree of nodes recursively until it returns nodes more simple. Then, I create the last object and while I come back of the recursively calls, I create the objects more complex until I reached to the main object.
Until now, I have part of this code, that is the one wich have to parse the parts of the xml.
public static void readFile(String root){
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Scanner scanner = new Scanner(new File(root)).useDelimiter("\\Z");
String contents = scanner.next();
scanner.close();
Document document = builder.parse(new ByteArrayInputStream(contents.getBytes()));
Node node = null;
NodeList nodes = null;
Element element = document.getDocumentElement();
System.out.println(element.getNodeName());
NodeList subNodes;
NamedNodeMap attributes;
//if (element.hasAttributes())
visitNodes(element);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void visitNodes (Node node){
for(Node childNode = node.getFirstChild(); childNode!=null;){
if (childNode.getNodeType() == childNode.DOCUMENT_NODE){
System.out.println("Document node Name " + childNode.getNodeName());
visitNodes(childNode);
}else if (childNode.getNodeType() == childNode.ELEMENT_NODE){
System.out.println("Node Name " + childNode.getNodeName());
if (childNode.hasAttributes()){
visitAttributes(childNode.getAttributes());
}
if (childNode.hasChildNodes()){
visitNodes(childNode);
}
}else if (childNode.getNodeType() == childNode.TEXT_NODE && !childNode.getNodeValue().contains("\n\t")){
System.out.println("Node value " + childNode.getNodeValue());
}
Node nextChild = childNode.getNextSibling();
childNode = nextChild;
}
}
private static void visitAttributes(NamedNodeMap attributes){
Node node;
for(int i = 0; i < attributes.getLength(); i++){
node = attributes.item(i);
System.out.print(node.getNodeName() + " ");
System.out.print(node.getNodeValue() + " ");
}
}
I don't know the use of childNodeType. For example, I expected that the XML tags with childs in his structure, enter by the option NODE_DOCUMENT and the tags without childs by the ELEMENT_NODE.
But the most important problem I've found are the nodes [#text] because after one ELEMENT_NODE I always found this node and when I ask if the node hasChilds, always returns true by this node.
Has any option to obtain this text value, that finally I want to display without doing other recursively call when I enter into the ELEMENT_NODE option?
When one Node is of type DOCUMENT_NODE or DOCUMENT_COMMENT? My program always enter by the ELEMENT_NODE type
Have you any other suggestions? All the help or idea will be well received.
Thanks for all.