我需要将json转换为xml。
我正在使用org.json jar文件
但是,xml中元素的顺序是不同的,并且在转换时也缺少很少的元素。输入消息是带有json数组的嵌套json对象。 对于简单的json到xml,代码工作正常。 请为此问题提供备用方法/库
package jjson;
import java.io.*;
import org.json.JSONObject;
import org.json.XML;
public class Json {
public static void main (String args[]) {
InputStreamReader is = null;
BufferedReader br = null;
StringBuilder sb = null;
try {
File jsonFile = new File("<<provide location of file>>");
is = new InputStreamReader(new FileInputStream(jsonFile), "UTF-8");
// Read the input stream and covert into string object
//is = new InputStreamReader(inputStream);
sb = new StringBuilder();
br = new BufferedReader(is);
String read = br.readLine();
while (read != null) {
// System.out.println(read);
sb.append(read);
read = br.readLine();
}
// Convert the String object to XML and write in the default name
String xml = XML.toString(new JSONObject(sb.toString()));
System.out.println(xml);
//String service.write(xml.getBytes(), "default");
} catch (Exception e) {
// TODO Auto-generated catch block
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
编辑:
输入讯息
{
"Id": 1,
"Recipient": "xyz.ang@abcd.com",
"RequestedComments": null,
"GeneratedAutomatically": false,
"From": "Africa",
"To": "China",
"ResponsesPage": {
"Tables": [
{
"Name": null,
"Columns": [
"Question",
"Response"
],
"Rows": [
{
"Name": "1. General",
"Columns": [
{
"Key": "Question",
"Value": "1. General",
"Name": "SECTION_GENERAL",
"Type": null
},
{
"Key": "Response",
"Value": null,
"Name": null,
"Type": 5
}
],
"Status": 0,
"ValidationMessage": null
},
{
"Name": "1.1 Name of completing party",
"Columns": [
{
"Key": "Question",
"Value": "1.1 Name of completing party",
"Name": "COMPLETING_PARTY",
"Type": null
},
{
"Key": "Response",
"Value": "Captain",
"Name": null,
"Type": 0
}
],
"Status": 0,
"ValidationMessage": null
},
{
"Name": "1.2 Relationship",
"Columns": [
{
"Key": "Question",
"Value": "1.2 Relationship",
"Name": "RELATIONSHIP",
"Type": null
},
{
"Key": "Response",
"Value": "Owner",
"Name": null,
"Type": 1
}
],
"Status": 0,
"ValidationMessage": null
},
{
"Name": "2. Details",
"Columns": [
{
"Key": "Question",
"Value": "2. Details",
"Name": "SECTION_DETAILS",
"Type": null
},
{
"Key": "Response",
"Value": null,
"Name": null,
"Type": 5
}
],
"Status": 0,
"ValidationMessage": null
},
{
"Name": "2.1 FlatNumber",
"Columns": [
{
"Key": "Question",
"Value": "2.1 IMO Number",
"Name": "IMP_NUMBER",
"Type": null
},
{
"Key": "Response",
"Value": "9336012",
"Name": null,
"Type": 2
}
],
"Status": 0,
"ValidationMessage": null
}
]
},
{
"Name": "IDENTIFIER_GRID",
"Columns": [
"Question",
"Data",
"Correction if required"
],
"Rows": [
{
"Name": "2.2 Name",
"Columns": [
{
"Key": "Question",
"Value": "2.2 Name",
"Name": "TEXT_QUESTION",
"Type": null
},
{
"Key": "Data",
"Value": "IVS CABERNET",
"Name": null,
"Type": 0
},
{
"Key": "2.2 Name",
"Value": "",
"Name": null,
"Type": null
}
],
"Status": 0,
"ValidationMessage": null
},
{
"Name": "2.3",
"Columns": [
{
"Key": "Question",
"Value": "2.3",
"Name": "TEXT_QUESTION",
"Type": null
},
{
"Key": "Data",
"Value": "565598000",
"Name": null,
"Type": 0
},
{
"Key": "2.3 Maritime Mobile",
"Value": "",
"Name": null,
"Type": null
}
],
"Status": 0,
"ValidationMessage": null
},
{
"Name": "2.4 Build Date",
"Columns": [
{
"Key": "Question",
"Value": "2.4 Build Date",
"Name": "TEXT_QUESTION",
"Type": null
},
{
"Key": "RightShip Data",
"Value": "01 Oct 2007",
"Name": null,
"Type": 0
},
{
"Key": "2.4 Build Date",
"Value": "04 Oct. 2007",
"Name": null,
"Type": null
}
],
"Status": 0,
"ValidationMessage": null
}
]
},
{
"Name": null,
"Columns": [
"Question",
"Response"
],
"Rows": [
{
"Name": "2.5 Vessel Type ",
"Columns": [
{
"Key": "Question",
"Value": "2.5 Vessel Type ",
"Name": "SHIP_TYPE",
"Type": null
},
{
"Key": "Response",
"Value": "Bulk Carrier",
"Name": null,
"Type": 1
}
],
"Status": 0,
"ValidationMessage": null
}
]
},
{
"Name": "VESSEL_DETAILS_GRID",
"Columns": [
"Question",
"Data",
"Correction if required"
],
"Rows": [
{
"Name": "10. Attachments",
"Columns": [
{
"Key": "Question",
"Value": "10. Attachments",
"Name": "SECTION_ATTACHMENTS",
"Type": null
},
{
"Key": "Response",
"Value": null,
"Name": null,
"Type": 5
}
],
"Status": 0,
"ValidationMessage": null
}
]
}
]
},
"QuestionsCount": 147,
"RulesCount": 5,
"PassedCount": 5,
"RequiredCount": 0,
"FCount": 0,
"VId": 190980,
"AId": null,
"AssignedTo": null,
"AssignedToName": null,
"AssignedToCustomer": null,
"AssignedOn": null,
"DecidedBy": "ABC",
"DecidedByName": "Red",
"DecidedByCustomerCode": "CSMC",
"DecidedByCustomerName": "CSG",
"DecidedOn": "2017-05-02T15:14:18.893Z",
"DecidedComments": "Acceptable",
"DecidedAutomatically": false,
"Vessel": {
"Id": 190980,
"IMP": 1234,
"PrId": "12345",
"Name": "IVS CABERNET",
"Ttatus": "In Service/Commission",
"VesselType": "Bulk Carrier",
"DWT": 177173,
"Age": 9.5,
"ActivityConfigList": null,
"Links": []
},
"Attachments": [],
"History": null,
"Comments": null,
"Reports": [],
"Links": []
}
预期输出XML :(对于样本添加JSON作为根元素)
<JSON>
<Id>1</Id>
<Recipient>xyz.ang@abcd.com</Recipient>
<RequestedComments/>
<GeneratedAutomatically>false</GeneratedAutomatically>
<From>Africa</From>
<To>China</To>
<ResponsesPage>
<Tables>
<Name/>
<Columns>Question</Columns>
<Columns>Response</Columns>
<Rows>
<Name>1. General</Name>
<Columns>
<Key>Question</Key>
<Value>1. General</Value>
<Name>SECTION_GENERAL</Name>
<Type/>
</Columns>
<Columns>
<Key>Response</Key>
<Value/>
<Name/>
<Type>5</Type>
</Columns>
<Status>0</Status>
<ValidationMessage/>
</Rows>
<Rows>
<Name>1.1 Name of completing party</Name>
<Columns>
<Key>Question</Key>
<Value>1.1 Name of completing party</Value>
<Name>COMPLETING_PARTY</Name>
<Type/>
</Columns>
<Columns>
<Key>Response</Key>
<Value>Captain</Value>
<Name/>
<Type>0</Type>
</Columns>
<Status>0</Status>
<ValidationMessage/>
</Rows>
<Rows>
<Name>1.2 Relationship</Name>
<Columns>
<Key>Question</Key>
<Value>1.2 Relationship</Value>
<Name>RELATIONSHIP</Name>
<Type/>
</Columns>
<Columns>
<Key>Response</Key>
<Value>Owner</Value>
<Name/>
<Type>1</Type>
</Columns>
<Status>0</Status>
<ValidationMessage/>
</Rows>
<Rows>
<Name>2. Details</Name>
<Columns>
<Key>Question</Key>
<Value>2. Details</Value>
<Name>SECTION_DETAILS</Name>
<Type/>
</Columns>
<Columns>
<Key>Response</Key>
<Value/>
<Name/>
<Type>5</Type>
</Columns>
<Status>0</Status>
<ValidationMessage/>
</Rows>
<Rows>
<Name>2.1 FlatNumber</Name>
<Columns>
<Key>Question</Key>
<Value>2.1 IMO Number</Value>
<Name>IMP_NUMBER</Name>
<Type/>
</Columns>
<Columns>
<Key>Response</Key>
<Value>9336012</Value>
<Name/>
<Type>2</Type>
</Columns>
<Status>0</Status>
<ValidationMessage/>
</Rows>
</Tables>
<Tables>
<Name>IDENTIFIER_GRID</Name>
<Columns>Question</Columns>
<Columns>Data</Columns>
<Columns>Correction if required</Columns>
<Rows>
<Name>2.2 Name</Name>
<Columns>
<Key>Question</Key>
<Value>2.2 Name</Value>
<Name>TEXT_QUESTION</Name>
<Type/>
</Columns>
<Columns>
<Key>Data</Key>
<Value>IVS CABERNET</Value>
<Name/>
<Type>0</Type>
</Columns>
<Columns>
<Key>2.2 Name</Key>
<Value/>
<Name/>
<Type/>
</Columns>
<Status>0</Status>
<ValidationMessage/>
</Rows>
<Rows>
<Name>2.3</Name>
<Columns>
<Key>Question</Key>
<Value>2.3</Value>
<Name>TEXT_QUESTION</Name>
<Type/>
</Columns>
<Columns>
<Key>Data</Key>
<Value>565598000</Value>
<Name/>
<Type>0</Type>
</Columns>
<Columns>
<Key>2.3 Maritime Mobile</Key>
<Value/>
<Name/>
<Type/>
</Columns>
<Status>0</Status>
<ValidationMessage/>
</Rows>
<Rows>
<Name>2.4 Build Date</Name>
<Columns>
<Key>Question</Key>
<Value>2.4 Build Date</Value>
<Name>TEXT_QUESTION</Name>
<Type/>
</Columns>
<Columns>
<Key>RightShip Data</Key>
<Value>01 Oct 2007</Value>
<Name/>
<Type>0</Type>
</Columns>
<Columns>
<Key>2.4 Build Date</Key>
<Value>04 Oct. 2007</Value>
<Name/>
<Type/>
</Columns>
<Status>0</Status>
<ValidationMessage/>
</Rows>
</Tables>
<Tables>
<Name/>
<Columns>Question</Columns>
<Columns>Response</Columns>
<Rows>
<Name>2.5 Vessel Type </Name>
<Columns>
<Key>Question</Key>
<Value>2.5 Vessel Type </Value>
<Name>SHIP_TYPE</Name>
<Type/>
</Columns>
<Columns>
<Key>Response</Key>
<Value>Bulk Carrier</Value>
<Name/>
<Type>1</Type>
</Columns>
<Status>0</Status>
<ValidationMessage/>
</Rows>
</Tables>
<Tables>
<Name>VESSEL_DETAILS_GRID</Name>
<Columns>Question</Columns>
<Columns>RightShip Data</Columns>
<Columns>Correction if required</Columns>
<Rows>
<Name>10. Attachments</Name>
<Columns>
<Key>Question</Key>
<Value>10. Attachments</Value>
<Name>SECTION_ATTACHMENTS</Name>
<Type/>
</Columns>
<Columns>
<Key>Response</Key>
<Value/>
<Name/>
<Type>5</Type>
</Columns>
<Status>0</Status>
<ValidationMessage/>
</Rows>
</Tables>
</ResponsesPage>
<QuestionsCount>147</QuestionsCount>
<RulesCount>5</RulesCount>
<PassedCount>5</PassedCount>
<RequiredCount>0</RequiredCount>
<FCount>0</FCount>
<VId>190980</VId>
<AId/>
<AssignedTo/>
<AssignedToName/>
<AssignedToCustomer/>
<AssignedOn/>
<DecidedBy>ABC</DecidedBy>
<DecidedByName>Red</DecidedByName>
<DecidedByCustomerCode>CSMC</DecidedByCustomerCode>
<DecidedByCustomerName>CSG</DecidedByCustomerName>
<DecidedOn>2017-05-02T15:14:18.893Z</DecidedOn>
<DecidedComments>Acceptable</DecidedComments>
<DecidedAutomatically>false</DecidedAutomatically>
<Vessel>
<Id>190980</Id>
<IMP>1234</IMP>
<PrId>12345</PrId>
<Name>IVS CABERNET</Name>
<Ttatus>In Service/Commission</Ttatus>
<VesselType>Bulk Carrier</VesselType>
<DWT>177173</DWT>
<Age>9.5</Age>
<ActivityConfigList/>
<Links/>
</Vessel>
<Attachments/>
<History/>
<Comments/>
<Reports/>
<Links/>
</JSON>
谢谢, Krish
答案 0 :(得分:0)
以下XSLT 3.0样式表提供了您正在寻找的输出(它使用json-to-xml()函数进行XML的通用转换,然后将通用XML转换为您需要的特定表单):
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
expand-text="yes"
exclude-result-prefixes="#all"
version="3.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template name="xsl:initial-template">
<JSON>
<xsl:apply-templates select="json-to-xml(unparsed-text('test.json'))"/>
</JSON>
</xsl:template>
<xsl:template match="*[@key[. castable as xs:NCName]]" priority="2">
<xsl:element name="{@key}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="*[@key[not(. castable as xs:NCName)]]" priority="2">
<xsl:message terminate="yes">Key {@key} cannot be used as an element name</xsl:message>
</xsl:template>
<xsl:template match="fn:array[@key[. castable as xs:NCName]][exists(*)]" priority="3">
<xsl:for-each select="*">
<xsl:element name="{../@key}">
<xsl:apply-templates select="."/>
</xsl:element>
</xsl:for-each>
</xsl:template>
<xsl:template match="fn:array[@key[. castable as xs:NCName]][empty(*)]" priority="3">
<xsl:element name="{@key}"/>
</xsl:template>
</xsl:stylesheet>