Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7950309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:13:48+00:00 2026-06-04T02:13:48+00:00

im trying to convert a general tree (unlimited child nodes) associate to an XML

  • 0

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:

  • 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 :

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);

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:13:49+00:00Added an answer on June 4, 2026 at 2:13 am

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to convert a general tree (tree that have one or more children
Trying to convert this c code into MIPS and run it in SPIM. int
I am trying to convert xsd file to the .net class. I have searched
I'm trying convert xml from one format to other using the XslCompiledTransform in c#.
I'm trying to convert a two-dimensional array into a structured array with named fields.
Im trying to convert this php function to javascript: function sanitize_words($string,$limit=false) { preg_match_all(/\p{L}[\p{L}\p{Mn}\p{Pd}'\x{2019}]{1,}/u,$string,$matches,PREG_PATTERN_ORDER); return
i´m using subsonic 3 trying convert a SQL2008 project to MySQL. when the projects
Trying to convert the following but am having difficulty. Can anyone see where I'm
Trying to convert int arrays to string arrays in numpy In [66]: a=array([0,33,4444522]) In
Im trying to convert unixtime to date but the results im getting are wrong

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.