itro
itro

Reputation: 7228

How to make Java object from XML?

I have a lot of XML files as below. How can I create Java class (Java object) from it automatically? How to update key and value in the String?

`<?xml version="1.0" encoding="utf-8" ?>
   <configuration>
     <appSettings>
       <add key="MODE" value="1"/>   
       <add key="NAME" value="DIRN2"/>
   </appSettings>
</configuration>`

public void createPc("pc.xml"){
      XStream xstream = new XStream();
      Pc newpc = (Pc)xstream.fromXML(pc.xml);

     // updating key and value
       ????????????
 }

Upvotes: 1

Views: 1172

Answers (2)

NiranjanBhat
NiranjanBhat

Reputation: 1832

Use JaxB. Inorder to create objects, using JaxB, all you need to do is define XSD for your XMLs and generate the classes for your XSD.

Upvotes: 1

Pau
Pau

Reputation: 803

http://www.castor.org/

Creates automatically the classes you need from the xmls.

You need to marshall and unmarshall it to populate the java objects and update the xml file.

Upvotes: 0

Related Questions