【发布时间】:2019-11-19 21:42:24
【问题描述】:
我正在尝试使用 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/_xv6jsJceEeimbPqnRT_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/JLO1COB">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/_xv6jsJceEeimbPqnG_Q/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 进行转换,但它没有提供正确的 json,其中 jsonobject 键/值具有命名空间。
我尝试过的代码没有给出响应:
JSONObject jsonObject = XML.toJSONObject("xml");
我希望有一种转换方式可以为 Json 提供正确的格式和具有命名空间的 json 对象,如果我隐藏这个 json 它应该给出初始 xml 请帮帮我。
【问题讨论】:
-
我的意思是,JSON 中没有命名空间之类的东西,所以...
-
json 中没有命名空间之类的东西。但是如果我们将 xml 转换为 json..json 对象可以有命名空间。
-
我不明白你的意思。你应该提供例子。但是,当某些东西一开始就没有命名空间时,就没有什么神奇的情况可以让它现在有了命名空间。 JSON 没有命名空间。所以,没有什么能让它拥有命名空间。
-
是的,我知道,json 不会有命名空间。但是 jsonObject 或 keys 可以是正确的命名空间。我已经给出了上面的 xml,并且 xml 有命名空间。我需要将此 xml 转换为 json 并修改 json 中的某些值并将其转换回来。因此,当我将其转换回 xml 时,我需要正确地恢复命名空间以使其成为有效的 json
-
命名空间本质上是元素名称的一部分:它们总是相同的。不要将命名空间放在 JSON 中,而只使用足够独特的名称,无需在命名空间中分隔它们。然后在将其转换回 XML 时,将名称空间放回去。没有库会为您进行这样的转换,首先从 XML 转换为对象,然后从对象转换为 JSON,反之亦然。
标签: java json xml namespaces converters