开发者

Converting a raw file (binary data ) into XML file

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 开发者_如何学Pythonthe XML document ?


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.


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜