我正在尝试使用java将xml转换为json,然后在修改后将json转换回xml,这应该给出相同的xml。 xml 属性具有命名空间。
我的示例 xml :
<?xml version="1.0" encoding="UTF-8"?>
<ns2:testplan xmlns:ns2="http://jazz.net/xmlns/alm/qm/v0.1/" xmlns:ns1="http://schema.ibm.com/vega/2008/" xmlns:ns3="http://purl.org/dc/elements/1.1/" xmlns:ns4="http://jazz.net/xmlns/prod/jazz/process/0.6/" xmlns:ns5="http://jazz.net/xmlns/alm/v0.1/" xmlns:ns6="http://purl.org/dc/terms/" xmlns:ns7="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:ns8="http://jazz.net/xmlns/alm/qm/v0.1/testscript/v0.1/" xmlns:ns9="http://jazz.net/xmlns/alm/qm/v0.1/executionworkitem/v0.1" xmlns:ns10="http://open-services.net/ns/core#" xmlns:ns11="http://open-services.net/ns/qm#" xmlns:ns12="http://jazz.net/xmlns/prod/jazz/rqm/process/1.0/" xmlns:ns13="http://www.w3.org/2002/07/owl#" xmlns:ns14="http://jazz.net/xmlns/alm/qm/qmadapter/v0.1" xmlns:ns15="http://jazz.net/xmlns/alm/qm/qmadapter/task/v0.1" xmlns:ns16="http://jazz.net/xmlns/alm/qm/v0.1/executionresult/v0.1" xmlns:ns17="http://jazz.net/xmlns/alm/qm/v0.1/catalog/v0.1" xmlns:ns18="http://jazz.net/xmlns/alm/qm/v0.1/tsl/v0.1/" xmlns:ns20="http://jazz.net/xmlns/alm/qm/styleinfo/v0.1/" xmlns:ns21="http://www.w3.org/1999/XSL/Transform">
<ns2:projectArea href="https://testserver:9080/qm/resource/itemOid/com.ibm.team.process.ProjectArea/_xv6jsJceEeimRT_G_Q" alias="projectArea"/>
<ns3:identifier>https://testserver:9080/qm/service/com.ibm.rqm.integration.service.IIntegrationService/resources/projectArea/testplan/urn:com.ibm.rqm:testplan:70?revision=294</ns3:identifier>
<ns2:stylesheet href="https://testserver:9080/qm/service/com.ibm.rqm.integration.service.IIntegrationService/resources/projectArea/testplan/urn:com.ibm.rqm:testplan:70?stylesheet=true"/>
<ns2:snapshot>
<ns3:title>testplan_70_<Reason>_<Version>_on_16 Apr 2019 05:50</ns3:title>
<ns5:updated>2019-04-16T12:20:01.644Z</ns5:updated>
<ns2:revision>294</ns2:revision>
</ns2:snapshot>
<ns2:webId>70</ns2:webId>
<ns3:title>Demo test plan 06</ns3:title>
<ns3:description/>
<ns2:creationDate>2019-01-22T10:36:40.289Z</ns2:creationDate>
<ns5:updated>2019-04-16T12:20:01.644Z</ns5:updated>
<ns5:state ns7:resource="https://testserver:9080/qm/service/com.ibm.rqm.integration.service.IIntegrationService/process-info/_xv6jsJceEeRT_G_Q/workflowstate/com.ibm.rqm.process.testplan.workflow/com.ibm.rqm.planning.common.underreview">com.ibm.rqm.planning.common.underreview</ns5:state>
<ns3:creator ns7:resource="https://testserver:9080/jts/resource/itemName/com.ibm.team.repository.Contributor/ABB">abc</ns3:creator>
<ns5:owner>unassigned</ns5:owner>
<ns2:priority ns7:resource="https://testserver:9080/qm/service/com.ibm.rqm.integration.service.IIntegrationService/process-info/_xv6jsJceEeimbPqQ/priority/literal.priority.101">literal.priority.101</ns2:priority>
<ns2:locked>false</ns2:locked>
<ns2:component href="https://testserver:9080/qm/service/com.ibm.rqm.integration.service.IIntegrationService/resources/projectArea/component/_yzQ3EZcmbPqnRT_G_Q"/>
</ns2:testplan>
有人可以帮我使用java进行转换吗?
我尝试使用 org.json.XML 进行转换,但它没有提供具有命名空间的 jsonobject 键/值的正确 json。
我尝试过但没有给出回应的代码:
JSONObject jsonObject = XML.toJSONObject("xml");
我期望一种转换方式,为 Json 提供正确的格式和具有命名空间的 json 对象,如果我隐藏此 json,它应该给出初始 xml 请帮助我。
您可以尝试 jackson 的 XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper)
XmlMapper xmlMapper = new XmlMapper();
JsonNode jsonNode = xmlMapper.readTree(string.getBytes());
ObjectMapper objectMapper = new ObjectMapper();
String value = objectMapper.writeValueAsString(jsonNode);
编辑:我使用过的依赖项
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.0</version>
</dependency>
这个问题有点老了,但我想分享我最近完成的同一任务的答案! 要求是: 有了这个 xml:
<ns2:testplan>
<ns2:snapshot>
<ns2:revision>294</ns2:revision>
</ns2:snapshot>
<ns2:webId>70</ns2:webId>
<ns2:title>Demo test plan 06</ns2:title>
</ns2:testplan>
我们想将其转换为:
{
"ns2:testplan": {
"ns2:snapshot": {
"ns2:revision": 294
},
"ns2:title": "Demo test plan 06",
"ns2:webId": 70
}
}
请注意,对于接受的响应,根标签
<ns2:testplan>
和命名空间前缀不包含在 JSON 中。
要配置 XmlMapper
以保留命名空间并添加根标签,请按以下步骤操作:
var module = new SimpleModule().addDeserializer(JsonNode.class, new JsonNodeDeserializer() {
@Override
public JsonNode deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return ctxt.getNodeFactory()
.objectNode()
.set("ns2:testplan", super.deserialize(p, ctxt)); // this config adds the root tag
}
});
XMLInputFactory xmlInputFactory = new WstxInputFactory();
xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false); // this one keeps the namespace prefix
XmlMapper xmlMapper = new XmlMapper(new XmlFactory(xmlInputFactory, new WstxOutputFactory()));
xmlMapper.registerModule(module);
然后使用接受的答案的代码将 XML 转换为 JSON。
获取
JSONObject
并将其转换为JSON字符串:
JSONObject jsonObject = XML.toJSONObject("xml");
String jsonString = jsonObject.toString();
如果你想漂亮地打印字符串,请使用
toString(int)
,它将空格数作为参数。
String prettyPrintedJson = jsonObject.toString(4);