我有一个 xml 架构 -
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.tibco.com/schemas/RegTestingStub/SharedResources/SchemaDefinitions/CommonXSD/Schema.xsd2"
targetNamespace="http://www.tibco.com/schemas/RegTestingStub/SharedResources/SchemaDefinitions/CommonXSD/Schema.xsd2"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="AccumulateResponse" type="AccumulateResponse"/>
<xs:complexType name="AccumulateResponse">
<xs:sequence>
<xs:element name="TestCase" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Transactionid" type="xs:string"/>
<xs:element name="TransactionType" type="xs:string"/>
<xs:element name="Status" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
我正在按照以下步骤进行此转换 - 1)使用XJC从XML Schema生成Java类 2)使用Java编译器API编译类 3)通过JAXB解码类实例
但都是重IO操作。有没有办法在内存中做到这一点?
最佳答案
您可以使用http://www.xsd2xml.com/用于从 xsd 生成示例 xml,之后您可以使用 site: http://www.utilities-online.info/xmltojson/将 xml 转换为 json。
对于你的 xsd,我得到了示例 xml:
<?xml version="1.0" encoding="utf-8"?>
<AccumulateResponse>
<TestCase>
<Transactionid>str1234</Transactionid>
<TransactionType>str1234</TransactionType>
<Status>str1234</Status>
</TestCase>
<TestCase>
<Transactionid>str5678</Transactionid>
<TransactionType>str5678</TransactionType>
<Status>str5678</Status>
</TestCase>
</AccumulateResponse>
并使用我得到的第二个网站:
{
"AccumulateResponse": {
"TestCase": [
{
"Transactionid": "str1234",
"TransactionType": "str1234",
"Status": "str1234"
},
{
"Transactionid": "str5678",
"TransactionType": "str5678",
"Status": "str5678"
}
]
}
}
关于java - XML 到 JSON 转换问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26137303/