Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在将JSON转换为XML时添加额外的元数据_Java_Json_Xml_Underscore Java - Fatal编程技术网

Java 在将JSON转换为XML时添加额外的元数据

Java 在将JSON转换为XML时添加额外的元数据,java,json,xml,underscore-java,Java,Json,Xml,Underscore Java,我想将JSON响应转换为基于SOAP的XML响应(使用) 导入com.github.underline.lodash.U; 公共类JsonToXml{ 公共静态void main(字符串[]args){ “人口”方面的“人口”方面的:“人口”方面的::“43.2000\””,,,,“人口”方面的:,,,,,,,,“身份”方面的,”id\:“2\”,,,“名称”方面的:,“2\”,,,“名称”方面的:“,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

我想将JSON响应转换为基于SOAP的XML响应(使用)

导入com.github.underline.lodash.U;
公共类JsonToXml{
公共静态void main(字符串[]args){
“人口”方面的“人口”方面的:“人口”方面的::“43.2000\””,,,,“人口”方面的:,,,,,,,,“身份”方面的,”id\:“2\”,,,“名称”方面的:,“2\”,,,“名称”方面的:“,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,“,”人口“:\”3971000\“},{\”身份证“:\”6\“,”姓名“:”纽约“,”人口“:”8550000“,”身份证“:”7“,”姓名“:”爱丁堡“,”人口“:”464000“,”身份证“:”8“,”姓名“:”柏林“,”人口“:”3671000“;
字符串jsonToXml=U.jsonToXml(json);
System.out.println(jsonToXml);
}
}
在将JSON转换为XML时,如何添加以下内容以响应XML


代码:

    String xml ="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
        + "   <soapenv:Header/>"
        + "   <soapenv:Body><Cities><City></City></Cities>"
        + "   <OperationResponse> <Type>SUCCESS</Type> <Code>0</Code> <ResponseDetails> <ResponseDetail> <Message>Operation completed successfully</Message> </ResponseDetail> </ResponseDetails> </OperationResponse>"
        + "   </soapenv:Body>"
        + "</soapenv:Envelope>";
    Map<String, Object> map = (Map<String, Object>) U.fromXml(xml);
    String json = "[{\"id\":\"1\",\"name\":\"Bratislava\",\"population\":\"432000\"},{\"id\":\"2\",\"name\":\"Budapest\",\"population\":\"1759000\"},{\"id\":\"3\",\"name\":\"Prague\",\"population\":\"1280000\"},{\"id\":\"4\",\"name\":\"Warsaw\",\"population\":\"1748000\"},{\"id\":\"5\",\"name\":\"Los Angeles\",\"population\":\"3971000\"},{\"id\":\"6\",\"name\":\"New York\",\"population\":\"8550000\"},{\"id\":\"7\",\"name\":\"Edinburgh\",\"population\":\"464000\"},{\"id\":\"8\",\"name\":\"Berlin\",\"population\":\"3671000\"}]";
    List<Object> list = (List<Object>) U.fromJson(json);
    U.set(map, "soapenv:Envelope.soapenv:Body.Cities.City", list);
    System.out.println(U.toXml(map));
输出:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Header/>
  <soapenv:Body>
    <Cities>
      <City>
        <id>1</id>
        <name>Bratislava</name>
        <population>432000</population>
      </City>
      <City>
        <id>2</id>
        <name>Budapest</name>
        <population>1759000</population>
      </City>
      <City>
        <id>3</id>
        <name>Prague</name>
        <population>1280000</population>
      </City>
      <City>
        <id>4</id>
        <name>Warsaw</name>
        <population>1748000</population>
      </City>
      <City>
        <id>5</id>
        <name>Los Angeles</name>
        <population>3971000</population>
      </City>
      <City>
        <id>6</id>
        <name>New York</name>
        <population>8550000</population>
      </City>
      <City>
        <id>7</id>
        <name>Edinburgh</name>
        <population>464000</population>
      </City>
      <City>
        <id>8</id>
        <name>Berlin</name>
        <population>3671000</population>
      </City>
    </Cities>
    <OperationResponse>
      <Type>SUCCESS</Type>
      <Code>0</Code>
      <ResponseDetails>
        <ResponseDetail>
          <Message>Operation completed successfully</Message>
        </ResponseDetail>
      </ResponseDetails>
    </OperationResponse>
  </soapenv:Body>
</soapenv:Envelope>

同样,如何将下面的内容附加到输出?
SUCCESS
0
操作已成功完成
您可以将此字符串添加到XML模板。我们是否可以添加位和段,并在需要时使用它?是的,我们可以在一个映射上合并XML和JSON。