Reputation: 1677
I'm currently learning Java and how to deal with XML data. I've been learning how to use the Java SAX to parse my xml data to java objects.This XML document can change and have additional children added to it (For example: Birthday, height...). So what is the best recommendation to handle this XML document? I was told to use objects like this:
Object1.ID
Object1.Emp_Id
Object1.Emp_Name
...
Object2.ID
Object2.Emp_Id
Object2.Emp_Name
If the XML received a new child like Birthday, then the app will add it to the object as such:
Object1.ID
Object1.Emp_Id
Object1.Emp_Name
Object1.Birthday
Could someone point me to the right direction where I can dynamically create new objects like the example above that I can drop the child nodes into? So if the child nodes were to change, I don't have to directly specify it? Sorry for the noob talk, I'm not sure If I'm explaining this right. I'm learning SAX and found this tutorial, but doesn't seem to explain what I want to do: Mapping XML to Java Objects
Thank yoU!
XML file:
<?xml version = "1.0" ?>
<Employee-Detail>
<Employee>
<ID no="1">
<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> Vinod1@yahoo.com </Emp_E-mail>
<Sex>Male</Sex>
<Age>25</Age>
</ID>
</Employee>
<Employee>
<ID no="2">
<Emp_Id> E-002 </Emp_Id>
<Emp_Name> Amit </Emp_Name>
<Emp_E-mail> Amit2@yahoo.com </Emp_E-mail>
<Sex>Male</Sex>
<Age>21</Age>
</ID>
</Employee>
</Employee-Detail>
Upvotes: 4
Views: 8739
Reputation: 14198
Another option is XStream: http://x-stream.github.io/. It is less flexible than JAXB, but it has the advantage that you don't need much configuration.
In your case you'd just create a class:
public class Employee {
String Id;
String EmpId;
...
}
XStream xstream = new XStream();
xstream.alias("Employee", Employee.class);
xstream.useAttributeFor(Employee.class, "ID");
xstream.aliasField("no", Blog.class, "ID");
Employee e = (Employee)xstream.fromXML(xml);
It is a bit of a pain, though, if your XML doesn't map cleanly to your objects.
Upvotes: 1
Reputation: 674
I personally really enjoy using JAXB.
You can take that sample XML and pass it to a XML schema (XSD) generator. Then take the XSD and generate your Java objects using the XJC tool.
Once you have your Java objects generated, you can marshal (save/export/write) the XML or unmarshal (load/import/read) it using the Marshaller and Unmarshaller classes respectively.
There used to be an online XML to XSD converter at http://www.flame-ware.com/products/xml-2-xsd/ but it seems to be down at the moment. You can download an express version of Visual Studio for free to accomplish the same thing, though.
I like this tutorial for JAXB: http://download.oracle.com/javaee/5/tutorial/doc/bnbah.html
I usually use SAX parsing only if I want full control over everything that's parsed as I'm parsing (hardly ever).
I don't enjoy using the DOM method because it requires the entire XML document to be parsed before you can actually do anything with it and once it's parsed, you need to do more work to configure your parser/XML consumer to know what the fields are to actually do anything with it.
As for your dynamic children, I would simply include in the schema an uncapped number of objects representing key-value pairs. It'll still save you time due to all the code generated for you.
The XmlBeans library is similar to JAXB, but I prefer the latter since it was inducted into Java's core release while XmlBeans was not.
Upvotes: 4
Reputation: 81724
There are basically two ways to do this. First, you can define Java classes whose names map onto the XML elements. You can do it by hand, or you can use a tool like XmlBeans (there are many others.) Both are equivalent: you have to know something about the XML in advance.
The other way to do it is not to use dedicated classes, but to use some kind of generic "tree node" objects which have a "name" property, a map of attributes, and a list of children. The Document Object Model (DOM) is a standard version of this, and in fact, using a DOM parser which builds this tree for you automatically is a standard alternative to using SAX. You could also create your own generic element class, but you should probably try it using DOM first, to see how it works for your application.
Upvotes: 1