Java: reading xml with many nested elements
<p>I have xml that looks somewhat similar to the following. I need to get values from attributes and tags and store them. But i cant understand how to get to level 2(3,4, etc) nested elements. I use following code that i found on the internet, it uses <strong>DOM</strong> but i cant get correct RegNumber and its attributes. Thanks in advance.</p>

<blockquote>
<pre><code>nodeList = xmldocument.getElementsByTagName("Header");      
        if (nodeList != null &amp;&amp; nodeList.getLength() &gt; 0) {
            for (int i = 0; i &lt; nodeList.getLength(); i++) {
                //get the header element
                Element header = (Element) nodeList.item(i);
                System.out.println("Element: " + ((Element)nodeList.item(i)).getNodeName());
                System.out.println(header.getAttribute("time"));
            }
        }
        nodeList = xmldocument.getElementsByTagName("Document");      
        if (nodeList != null &amp;&amp; nodeList.getLength() &gt; 0) {
            for (int i = 0; i &lt; nodeList.getLength(); i++) {

                //get the document element
                Element document = (Element) nodeList.item(i);
                System.out.println("Element: " + ((Element)nodeList.item(i)).getNodeName());
                System.out.println(document.getAttribute("Id"));

                nodeList = document.getElementsByTagName("RegNumber"); 
                for (int j = 0; j &lt; nodeList.getLength(); j++) {

                    //get the RegNumber element
                    Element regNumber = (Element) nodeList.item(j);
                    System.out.println("Element: " + ((Element)nodeList.item(j)).getNodeName());
                    System.out.println(regNumber.getAttribute("regpoint"));

                }
            }

        }
</code></pre>
</blockquote>

<p>xml:  </p>

<pre><code>&lt;XML xsi:schemaLocation="http://www.codetools.it XSD2.xsd" xmlns="http://www.codetools.it" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
            &lt;Header time="2001-12-17T09:30:47Z" /&gt;
            &lt;Document Id="456"&gt;
                &lt;RegNumber regpoint="" regdate="2001-12-17T09:30:47Z"&gt;123/456&lt;/RegNumber&gt;
                &lt;Confident flag="0"/&gt;
                &lt;DocNumber kind=""&gt;
                    &lt;RegNumber RegPoint="" regdate="2001-12-17T09:30:47Z"&gt;456/789&lt;/RegNumber&gt;
                    &lt;Organization fullname="lol" shortname="" ownership="lol" ogrn="78945612" inn=""&gt;
                        &lt;OfficialPerson&gt;
                            &lt;Name  Firstname="John"/&gt;
                            &lt;Official Department="" post=""/&gt;
                            &lt;SignDate&gt;2001-12-17T09:30:47Z&lt;/SignDate&gt;
                        &lt;/OfficialPerson&gt;
                        &lt;Econtact type="1"&gt;test@test.com.ua&lt;/Econtact&gt;
                        &lt;Address street="" settlement="" postcode="" postbox="" flat="" district="" region="" country="" nontypical="" house=""/&gt;
                    &lt;/Organization&gt;
                &lt;/DocNumber&gt;
                &lt;DocTransfer os="Windows" type=".docx" type_ver="" char_set="" description="kkkk"&gt;
    &lt;/DocTransfer&gt;
            &lt;Reghistory idnumber=""&gt;
                &lt;RegNumber RegPoint="" regdate=""/&gt;
                &lt;Organization fullname="" shortname="" ownership="" ogrn="" inn=""&gt;
                    &lt;Econtact type=""/&gt;
                    &lt;Address street="" settlement="" postcode="" postbox="" flat="" district="" region="" country="" nontypical="" house=""/&gt;
                &lt;/Organization&gt;
            &lt;/Reghistory&gt;
            &lt;Author&gt;
                &lt;Organization fullname="" shortname="" ownership="" ogrn="12345678" inn=""&gt;
                    &lt;OfficialPerson&gt;
                        &lt;Name  Firstname="" /&gt;
                        &lt;Official Department="" post=""/&gt;
                        &lt;SignDate&gt;2011-12-17T09:30:47Z&lt;/SignDate&gt;
                    &lt;/OfficialPerson&gt;
                    &lt;Econtact type="Рї"&gt;smirnov@test.com&lt;/Econtact&gt;
                    &lt;Address street=""  postcode="1234" postbox="" flat="" district="" region="" country="" nontypical="" house="12"/&gt;
                &lt;/Organization&gt;
                &lt;PrivatePerson&gt;
                    &lt;Name Surname="" Firstname="" Fathername=""/&gt;
                    &lt;Econtact type=""/&gt;
                    &lt;Address street="" settlement="" postcode="" postbox="" flat="" district="" region="" country="" nontypical="" house=""/&gt;
                    &lt;SignDate/&gt;
                    &lt;Rank privilege="" socialposition="" sex=""/&gt;
                &lt;/PrivatePerson&gt;
            &lt;/Author&gt;
            &lt;Validator attestation="0"&gt;
                &lt;DocNumber kind="РЅР°РєР°Р·"&gt;
                    &lt;RegNumber RegPoint="" regdate="2001-12-17T09:30:47Z"/&gt;
                    &lt;Organization fullname="" shortname="" ownership="" ogrn="14725836" inn=""&gt;
                        &lt;Econtact type="Рї"&gt;kmu@test.com&lt;/Econtact&gt;
                        &lt;Address street="" settlement="" postcode="" postbox="" flat="" district="" region="" country="" nontypical="" house=""/&gt;
                    &lt;/Organization&gt;
                &lt;/DocNumber&gt;
                &lt;Organization fullname="" shortname="РљРњРЈ" ownership="" ogrn="96325878" inn=""&gt;
                    &lt;Econtact type="String"&gt;kmu@test.com&lt;/Econtact&gt;
                    &lt;Address street="РџСѓС€РєС–РЅР°" settlement="" postcode="4563" postbox="" flat="" district="" region="" country="" nontypical="" house="23"/&gt;
                    &lt;OfficialPerson&gt;
                        &lt;Name Surname="" Firstname="" Fathername=""/&gt;
                        &lt;Official Department="" post=""/&gt;
                        &lt;SignDate&gt;2011-12-17T09:30:47Z&lt;/SignDate&gt;
                    &lt;/OfficialPerson&gt;
                &lt;/Organization&gt;
                &lt;PrivatePerson&gt;
                    &lt;Name Surname="" Firstname="" Fathername=""/&gt;
                    &lt;Econtact type=""&gt;&lt;/Econtact&gt;
                    &lt;Address street="" settlement="" postcode="" postbox="" flat="" district="" region="" country="" nontypical="" house=""/&gt;
                    &lt;SignDate&gt;&lt;/SignDate&gt;
                    &lt;Rank privilege="" socialposition="" sex=""/&gt;
                &lt;/PrivatePerson&gt;
            &lt;/Validator&gt;
            &lt;Addressee&gt;
                &lt;Referred id="" retype=""&gt;
                    &lt;RegNumber regdate="" regpoint=""/&gt;
                    &lt;TaskNumber taskDate=""/&gt;
                &lt;/Referred&gt;
                &lt;Organization fullname="String" shortname="" ownership="" ogrn="85236974" inn=""&gt;
                    &lt;OfficialPerson&gt;
                        &lt;Name Surname="" Firstname="" Fathername=""/&gt;
                        &lt;Official Department="" post=""/&gt;
                    &lt;/OfficialPerson&gt;
                    &lt;Econtact type="Рї"&gt;sofki@test.com&lt;/Econtact&gt;
                    &lt;Address street="" settlement="" postcode="" postbox="" flat="" district="" region="" country="" nontypical="" type="" house=""/&gt;
                &lt;/Organization&gt;
                &lt;PrivatePerson&gt;
                    &lt;Name Surname="" Firstname="" Fathername=""/&gt;
                    &lt;Econtact type=""&gt;&lt;/Econtact&gt;
                    &lt;Address street="" settlement="" postcode="" postbox="" flat="" district="" region="" country="" nontypical="" house=""/&gt;
                    &lt;Rank privilege="" socialposition="" sex=""/&gt;
                &lt;/PrivatePerson&gt;
            &lt;/Addressee&gt;
            &lt;Writer&gt;
                &lt;Organization fullname="" shortname="" ownership="" ogrn="" inn=""&gt;
                    &lt;OfficialPerson&gt;
                        &lt;Name Surname="" Firstname="" Fathername=""/&gt;
                        &lt;Econtact type=""&gt;&lt;/Econtact&gt;
                    &lt;/OfficialPerson&gt;
                &lt;/Organization&gt;
                &lt;PrivatePerson&gt;
                    &lt;Name Surname="" Firstname="" Fathername=""/&gt;
                    &lt;Econtact type=""&gt;&lt;/Econtact&gt;
                &lt;/PrivatePerson&gt;
            &lt;/Writer&gt;
        &lt;/Document&gt;
</code></pre>

<p><strong>edit</strong>. As advised i try to figure out JAXB thingy. xjc tool generated 3 classes: ObjectFactory, package-info, XML. I found this code to create object from xml:</p>

<pre><code>public static void main(String[] args) {
        try {

            File file = new File("F:\\Untitled3.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(XML.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            XML xml = (XML) jaxbUnmarshaller.unmarshal(file);
            System.out.println(xml);

        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
</code></pre>

<p>Is this the correct way, or i need to use ObjectFactory?:</p>

<pre><code>ObjectFactory f =new ObjectFactory();
            XML xml1 = f.createXML();
</code></pre>

<p>How do i create object with all tags and attributes, and then create xml file from it? Im new to java and jaxb stuff, so please bear with me.</p>
