Page 1 of 1

Writing XML files using Apache's XMLBeans sequel of read tutorial Rate Topic: -----

#1 bhandari  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 754
  • Joined: 31-January 08

Posted 07 March 2008 - 06:57 PM

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

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

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:
<?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 :)

Is This A Good Question/Topic? 0
  • +

Page 1 of 1