Wassim Sboui
Wassim Sboui

Reputation: 1820

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:

<?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 : enter image description here

Now i want to convert this tree to a binary tree, im aplying an algorithm for that:

So the result is :

enter image description here

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

Answers (1)

Dmitry
Dmitry

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

Related Questions