1

Hi all can any one tell me is it possible to convert any XML file file to equivalent java object using java?

5 Answers 5

2

You want a DOM parser. There are many around, a Google search for "Java DOM parser" will help you. Take this page for example.

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

2 Comments

Again in DOM also we are specifying root element name. But in my case i don't what kind of XML file user will give. I mean I will not be knowing contents of XML file. I am looking for soln which is can convert any XML structure to equivalent java object
You don't need to know what the contents is. You don't need to tell the parser the root element name. Read the page I linked and try it out, it describes exactly what you are looking for.
1

You are probably looking for JAXB.

2 Comments

JAXB is good if you have a specific XML schema that describes your documents and you want to generate schema-specific Java accessor objects.
But its not specific XML schema file. It can be any XML file.
1

Use XStream library it is quite simple: http://x-stream.github.io/tutorial.html

                // object -> XML -> File 
    XStream xstream = new XStream(driver);
    String data = xstream.toXML(metaData);

                //  XML -> object
    XStream xstream = new XStream(new JettisonMappedXmlDriver());
    YourClass obj = (UourClass)xstream.fromXML(jSON);

Comments

0

You could use unmarshall function in castor.

1 Comment

Yes. Castor mapping is easy option. You can transform Java Object from XMl as well as XML from Java object.
0

Let me add another to the collection.

Have a look at the Apache Jakarta Digester this is what Tomcat uses to automap XML files (like server.xml).

Comments

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.