Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,501 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,750 people online right now. Registration is fast and FREE... Join Now!




Writing XML files using Apache's XMLBeans

 
Reply to this topicStart new topic

> Writing XML files using Apache's XMLBeans, sequel of read tutorial

bhandari
Group Icon



post 7 Mar, 2008 - 05:57 PM
Post #1


Hi,

If you want to read xml files using Apache's XMLBeans, you may visit
here

Now lets see how to write to a xml file. If you have read the tutorial on how to read using xmlbeans, this tutorial is going to be a cakewalk for you.
Ok, lets get started.

The xml person.xml is same as before:
CODE

<?xml version="1.0" encoding="UTF-8"?>
<person-group>
    <person>
        <name>
            Mohan
        </name>
        <age>
            12
        </age>
    </person>
    <person>
        <name>
            Sohan
        </name>
        <age>
            15
        </age>
    </person>
    <person>
        <name>
            Rohan
        </name>
        <age>
            16
        </age>
    </person>
</person-group>


A person group has many persons and each person has name and age.
The corresponding XML Schema Diagram (XSD) will be:

CODE

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="person-group">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="person" type="person"/>
            </xs:sequence>
          </xs:complexType>
       </xs:element>
       <xs:complexType name="person">
       <xs:sequence>
               <xs:element name="name" type="xs:string"/>
               <xs:element name="age" type="xs:int"/>
       </xs:sequence>
       </xs:complexType>
</xs:schema>



Now we use scomp tool in XMLBeans to generate our java beans. The XMLBeans librray can be downloaded from here.

Extract the contents of this library and place the lib and bin folders to your CLASSPATH.

Go to command prompt and issue the following command:
E:\scomp –out e:\person.jar –compiler c:\j2sdk\bin\javac e:\person.xsd

The above command will generate a jar named as person.jar
Now include that jar in your CLASSPATH.

The code to write to person.xml is:

CODE

public class GeneratePage {
    public static void main(String args[]) {
       try {
            String filePath = "e:\\person.xml";
            File inputXMLFile = new File(filePath);
            PersonGroupDocument persGrpDoc = PersonGroupDocument.Factory.parse(inputXMLFile);
            PersonGroupDocument.PersonGroup prsGrp = persGrpDoc.getPersonGroup();
            Person pers = prsGrp.addNewPerson();
            pers.setName("Jane");
            pers.setAge(20);
            persGrpDoc.save(new File(filePath));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


In this code we are first creating an instance of the document by providing a file object for xml file and parsing it.
From the document, we get the Person Group instance
The person group object has a method to add a new person as you can see in the line "Person pers = prsGrp.addNewPerson();"
Now we need to set the name and age of the new person using the setters
The last step is to save this memory represntation of XML to physical .xml file.
We overwrite person.xml file using the save method of docment object as in line "persGrpDoc.save(new File(filePath));"

well that's it, Janes is now part of our person group.
The new contents of person.xml will be:
CODE

<?xml version="1.0" encoding="UTF-8"?>
<person-group>
    <person>
        <name>
            Mohan
        </name>
        <age>
            12
        </age>
    </person>
    <person>
        <name>
            Sohan
        </name>
        <age>
            15
        </age>
    </person>
    <person>
        <name>
            Rohan
        </name>
        <age>
            16
        </age>
    </person>
<person><name>Jane</name><age>20</age></person>
</person-group>


Now you see you can read and write files smile.gif
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 12/2/08 08:16PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month