4

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> [email protected] </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> [email protected] </Emp_E-mail>
<Sex>Male</Sex>
<Age>21</Age>
</ID>
</Employee>
</Employee-Detail>

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Question: Can I use like nested vectors with JAXB or SAX? I just found out that vectors can be named whatever and could hold whatever for each vector object. (Not sure if I'm explaining right). For example: Create one vector variable to hold one xml element parent name. Within that vector varialble I create another vector for the all the children within that parent. So I can achieve the Object1.child1, Object1.child2, Object1.child3...and so forth.
With JAXB you can also start from objects, this means a schema isn't required. Also Java is a standard (JSR-222), with multiple implementations (Metro, EclipseLink MOXy, Apache JaxMe), this is why it is included in Java SE 6. The development of this standard included a representative from XMLBeans as well as other object-to-XML tools.
JAXB natively supports maps of key value pairs. So yes, you can nest =). Take a look at XMLAdapter. Also, good addition, Blaise!
I just re-read your question. If you're just wondering if you can nest object types, that's also possible. Your top-level element would just be the XMLRootElement. So for example, you can have Company as the root level tag, and then a company can have many Employee child tags, each of which have multiple ContactInfo tags, etc.
1

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.

2 Comments

Isn't this the sort of thing where JAXB shines?
Thanks for the input, I will know how the elements will be configured, its just the child nodes I'm trying to solve. The child nodes names can be different because they will be created by the user, but all the parent nodes will be the same to help the application navigate teh document.
1

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.

3 Comments

But this method would mean that I would have to still know what child tags are in the xml file, right? I want to be able to dynamically create the objects based on the child tags in the file.
If you are mapping to a specific XML format there isn't much difference in configuration: bdoughan.blogspot.com/2010/10/…
I don't understand "Dynamically Create". As long as the child tags match existing classes/field name conventions, XStream will instantiate and map for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.