tags:

views:

108

answers:

4

Hello guyz, 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 ???

Thanks in Advance....

+1  A: 

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.

Carl Smotricz
A: 

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

Mustafa Magdy
+1  A: 

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.

Mite Mitreski
+1  A: 

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.

kdgregory