将原始文件(二进制数据)转换为XML文件

时间:2022-08-02 21:18:48

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

我正在开发一个项目,我必须从服务器获取一个原始文件并将其转换为XML文件。

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 ?

有没有java中的工具可以帮助我完成这个任务,就像JAXP可以用来解析XML文档一样?

4 个解决方案

#1


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

我想你需要你的对象供以后使用,所以创建MyObject,它将是你将从Raw文件中加载值的bean,你可以将它写入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.

或者如果你不需要后者使用的对象,你可以使用TransformerFactory。

#2


1  

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

是。这假设原始文件中的文本已经是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.

您从DocumentBuilderFactory开始获取DocumentBuilder,然后您可以使用其parse()方法将输入流转换为Document,这是一个内部XML表示。

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.

如果原始文件包含XML之外的其他内容,您将需要以某种方式扫描它(这里是您自己的代码)并使用您从空文档中构建的内容。

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.

然后,我通常使用TransformerFactory中的Transformer将Document转换为文件中的XML文本,但可能有一种更简单的方法。

#3


1  

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

JAXP还可用于创建新的空文档:

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

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

然后,您可以使用该Document创建元素,并根据需要附加它们:

    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.

但是,正如Jørn在对您的问题的评论中指出的那样,这一切都取决于您对这个“原始”文件的要求:如何将其转换为XML。只有你知道。

#4


0  

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

我想如果你试图在XmlDocument中加载它,这将没关系

#1


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

我想你需要你的对象供以后使用,所以创建MyObject,它将是你将从Raw文件中加载值的bean,你可以将它写入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.

或者如果你不需要后者使用的对象,你可以使用TransformerFactory。

#2


1  

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

是。这假设原始文件中的文本已经是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.

您从DocumentBuilderFactory开始获取DocumentBuilder,然后您可以使用其parse()方法将输入流转换为Document,这是一个内部XML表示。

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.

如果原始文件包含XML之外的其他内容,您将需要以某种方式扫描它(这里是您自己的代码)并使用您从空文档中构建的内容。

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.

然后,我通常使用TransformerFactory中的Transformer将Document转换为文件中的XML文本,但可能有一种更简单的方法。

#3


1  

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

JAXP还可用于创建新的空文档:

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

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

然后,您可以使用该Document创建元素,并根据需要附加它们:

    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.

但是,正如Jørn在对您的问题的评论中指出的那样,这一切都取决于您对这个“原始”文件的要求:如何将其转换为XML。只有你知道。

#4


0  

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

我想如果你试图在XmlDocument中加载它,这将没关系