Convert a General tree to a binary tree - XML Parser

im trying to convert a general tree (unlimited child nodes) associate to an XML file named "pays.xml" like this: \[code\]<?xml version="1.0" encoding="iso-8859-1"?><country> <name> </name> <city> </city> <region> <name> </name> <population> </population> <city> Lille </city> </region> <region> </region></country>\[/code\]the Tree associate to this Xml file :
WTdMv.png
Now i want to convert this tree to a binary tree, im aplying an algorithm for that:
  • use the root of the general tree as the root of the binary tree
  • determine the first child of the root. This is the leftmost node in thegeneral tree at the next level
  • insert this node. The child reference of the parent node refers to thisnode
  • continue finding the first child of each parent node and insert it belowthe parent node with the child reference of the parent to this node.
So the result is :
BuPob.png
So my problem is generating the XML file associate the the binary tree, the result i would like to have is :\[code\] <country> <name> <city> <region> <name> <population> <city> </city> </population> </name> <region></region> </region> </city> </name> </country>\[/code\]i have trying to write a code for that but unfortunately, i have this result \[code\] <?xml version="1.0" encoding="UTF-8" standalone="no"?><country><name/><city/><region><name/><population/><city/></region><region><name/><city/><city/></region></country>\[/code\]Here is my code :\[code\] public static Document Generer (Document doc, Node node,Node a ) { NodeList nl = node.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node n = nl.item(i); if (n instanceof Element) { Element b = doc.createElement(n.getNodeName()); //System.out.print(b); a.appendChild(b); Generer (doc,n,b); } } return doc;}public static void convert (Node node){ try { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); // root elements Document doc = docBuilder.newDocument(); Element a=doc.createElement(node.getNodeName()); doc.appendChild(a); doc=Generer (doc, node ,a); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); //StreamResult result = new StreamResult(new File("C:\\file.xml")); // Output to console for testing StreamResult result = new StreamResult(System.out); transformer.transform(source, result); } catch(Exception e) { e.printStackTrace(); }}public static Node GetNodeParent (String fichier1){ try{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder parser = factory.newDocumentBuilder(); Document doc = parser.parse(fichier1); Element root = doc.getDocumentElement(); return root; }catch (Exception e) { e.printStackTrace(); } return null;}public static void main(String[] args) { // TODO Auto-generated method stub Node n1= GetNodeParent("pays.xml"); //System.out.println(n1); convert(n1);}\[/code\]
 
Back
Top