Reputation: 1820
im trying to convert a general tree (unlimited child nodes) associate to an XML file named "pays.xml" like this:
<?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>
the Tree associate to this Xml file :
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 the general tree at the next level
insert this node. The child reference of the parent node refers to this node
continue finding the first child of each parent node and insert it below the parent node with the child reference of the parent to this node.
So the result is :
So my problem is generating the XML file associate the the binary tree, the result i would like to have is :
<country>
<name>
<city>
<region>
<name>
<population>
<city>
</city>
</population>
</name>
<region></region>
</region>
</city>
</name>
</country>
i have trying to write a code for that but unfortunately, i have this result
<?xml version="1.0" encoding="UTF-8" standalone="no"?><country><name/><city/><region><name/><population/><city/></region><region><name/><city/><city/></region></country>
Here is my 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);
}
Upvotes: 1
Views: 3815
Reputation: 1283
The code your provide do completly different thing, if you carefully look on your loop:
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);
}
}
You may notice that all child of "node" variable will be added to "a" element. So all you do just copy the full tree throwing away all not Element nodes (this leades to loosing formatting, for example).
Upvotes: 1