【问题标题】:Add extra metadata while converting JSON to XML在将 JSON 转换为 XML 时添加额外的元数据
【发布时间】:2020-04-15 09:40:39
【问题描述】:

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

    import com.github.underscore.lodash.U;

    public class JsonToXml {
        public static void main(String[] args) {
            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\"}]";

            String jsonToXml = U.jsonToXml(json);

            System.out.println(jsonToXml);
        }
    }

在将 JSON 转换为 XML 时,我们如何添加以下内容以响应 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>

【问题讨论】:

    标签: java json xml underscore-java


    【解决方案1】:

    代码:

        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>
    

    【讨论】:

    • 再一次,如何在下面附加到输出? &lt;OperationResponse&gt; &lt;Type&gt;SUCCESS&lt;/Type&gt; &lt;Code&gt;0&lt;/Code&gt; &lt;ResponseDetails&gt; &lt;ResponseDetail&gt; &lt;Message&gt;Operation completed successfully&lt;/Message&gt; &lt;/ResponseDetail&gt; &lt;/ResponseDetails&gt; &lt;/OperationResponse&gt;
    • 您可以将此字符串添加到 XML 模板中。
    • 我们不能添加一些零碎的东西并在需要的地方使用吗?
    • 是的,我们可以在一张地图上合并 XML 和 JSON。
    猜你喜欢
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 2022-08-03
    • 2013-03-26
    相关资源
    最近更新 更多