1

I'm working on a project under which i have to take a raw file from the server and convert it into XML file.

Is there any tool available in java which can help me to accomplish this task like JAXP can be used to parse the XML document ?

1
  • 5
    It really depends on what a "raw file" is in your application. Commented Jan 6, 2010 at 13:37

4 Answers 4

1

I guess you will need your objects for later use ,so create MyObject that will be some bean that you will load the values form your Raw File and you can write this to someFile.xml

FileOutputStream os = new FileOutputStream("someFile.xml");
XMLEncoder encoder = new XMLEncoder(os);
MyObject p = new MyObject();
p.setFirstName("Mite");
encoder.writeObject(p);
encoder.close();

Or you con go with TransformerFactory if you don't need the objects for latter use.

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

Comments

1

Yes. This assumes that the text in the raw file is already XML.

You start with the DocumentBuilderFactory to get a DocumentBuilder, and then you can use its parse() method to turn an input stream into a Document, which is an internal XML representation.

If the raw file contains something other than XML, you'll want to scan it somehow (your own code here) and use the stuff you find to build up from an empty Document.

I then usually use a Transformer from a TransformerFactory to convert the Document into XML text in a file, but there may be a simpler way.

Comments

1

JAXP can also be used to create a new, empty document:

    Document dom = DocumentBuilderFactory.newInstance()
                                         .newDocumentBuilder()
                                         .newDocument();

Then you can use that Document to create elements, and append them as needed:

    Element root = dom.createElement("root");
    dom.appendChild(root);

But, as Jørn noted in a comment to your question, it all depends on what you want to do with this "raw" file: how should it be turned into XML. And only you know that.

Comments

0

I think if you try to load it in an XmlDocument this will be fine

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.