将 XML 转换为 JSON 格式
我必须将 docx 文件格式(openXML 格式)转换为 JSON 格式。我需要一些指导方针来做到这一点。提前致谢。
I have to convert docx file format (which is in openXML format) into JSON format. I need some guidelines to do it. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以查看 Json-lib Java 库,它提供了 XML 到 - JSON 转换。
如果您也需要根标签,只需添加一个外部虚拟标签:
You may take a look at the Json-lib Java library, that provides XML-to-JSON conversion.
If you need the root tag too, simply add an outer dummy tag:
XML 和 JSON 之间没有直接映射; XML 带有类型信息(每个元素都有一个名称)以及命名空间。因此,除非每个 JSON 对象都嵌入了类型信息,否则转换将会是有损的。
但这并不一定重要。重要的是 JSON 的使用者知道数据契约。例如,给定此 XML:
您可以将其转换为:
并且使用者不需要知道
books
集合中的每个对象都是book
对象。编辑:
如果您有 XML 的 XML 架构并且正在使用 .NET,则可以使用 xsd.exe 从该架构生成类。然后,您可以将源 XML 解析为这些类的对象,然后使用 DataContractJsonSerializer 将类序列化为 JSON。
如果您没有架构,则很难自行手动定义 JSON 格式。
There is no direct mapping between XML and JSON; XML carries with it type information (each element has a name) as well as namespacing. Therefore, unless each JSON object has type information embedded, the conversion is going to be lossy.
But that doesn't necessarily matter. What does matter is that the consumer of the JSON knows the data contract. For example, given this XML:
You could convert it to this:
And the consumer wouldn't need to know that each object in the
books
collection was abook
object.Edit:
If you have an XML Schema for the XML and are using .NET, you can generate classes from the schema using xsd.exe. Then, you could parse the source XML into objects of these classes, then use a
DataContractJsonSerializer
to serialize the classes as JSON.If you don't have a schema, it will be hard getting around manually defining your JSON format yourself.
org.json 命名空间中的 XML 类 为您提供了此功能。
您必须调用静态 toJSONObject 方法< /a>
The XML class in the org.json namespace provides you with this functionality.
You have to call the static toJSONObject method
如果您对各种实现不满意,请尝试推出自己的实现。这是我今天下午编写的一些代码,以帮助您入门。它适用于 net.sf.json 和 apache common-lang:
以及 SAXJsonParser 实现:
If you are dissatisfied with the various implementations, try rolling your own. Here is some code I wrote this afternoon to get you started. It works with net.sf.json and apache common-lang:
And the SAXJsonParser implementation:
将完整的 docx 文件转换为 JSON 看起来不是一个好主意,因为 docx 是一种以文档为中心的 XML 格式,而 JSON 是一种以数据为中心的格式。一般来说,XML 被设计为以文档和数据为中心。尽管将以文档为中心的 XML 转换为 JSON 在技术上是可行的,但处理生成的数据可能过于复杂。尝试专注于实际需要的数据并仅转换该部分。
Converting complete docx files into JSON does not look like a good idea, because docx is a document centric XML format and JSON is a data centric format. XML in general is designed to be both, document and data centric. Though it is technical possible to convert document centric XML into JSON, handling the generated data might be overly complex. Try to focus on the actual needed data and convert only that part.
如果您需要能够在 XML 转换为 JSON 之前对其进行操作,或者想要对表示形式进行细粒度控制,请使用 XStream。在 xml 到对象、json 到对象、对象到 xml 和对象到 json 之间进行转换非常容易。以下是 XStream 文档 中的示例:
XML
POJO (DTO)
Convert from XML to POJO:
And then from POJO to JSON:
注意:虽然该方法读取
toXML()
XStream 将生成 JSON,因为使用了 Jettison 驱动程序。If you need to be able to manipulate your XML before it gets converted to JSON, or want fine-grained control of your representation, go with XStream. It's really easy to convert between: xml-to-object, json-to-object, object-to-xml, and object-to-json. Here's an example from XStream's docs:
XML
POJO (DTO)
Convert from XML to POJO:
And then from POJO to JSON:
Note: although the method reads
toXML()
XStream will produce JSON, since the Jettison driver is used.如果您有 xml 片段的有效 dtd 文件,那么您可以使用开源 eclipse 链接 jar 轻松将 xml 转换为 json,并将 json 转换为 xml。详细的示例 JAVA 项目可以在这里找到: http://www.cubicrace.com/2015/06/How-to-convert-XML-to-JSON-format.html
If you have a valid dtd file for the xml snippet, then you can easily convert xml to json and json to xml using the open source eclipse link jar. Detailed sample JAVA project can be found here: http://www.cubicrace.com/2015/06/How-to-convert-XML-to-JSON-format.html
用于
在生成的 JSON 中包含根元素。
你的代码会是这样的
Use
to include root element in resulting JSON.
Your code would be like this
Docx4j
我以前使用过docx4j,值得一看。
unXml
您还可以查看我的开源 unXml-库,该库位于 Maven 中心。
它是轻量级的,并且具有简单的语法来从 xml 中挑选 XPath,并将它们作为 中的 Json 属性返回杰克逊
ObjectNode
。Docx4j
I've used docx4j before, and it's worth taking a look at.
unXml
You could also check out my open source unXml-library that is available on Maven Central.
It is lightweight, and has a simple syntax to pick out XPaths from your xml, and get them returned as Json attributes in a Jackson
ObjectNode
.