使用 Java 将 xml 转换为 json

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28451370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:15:18  来源:igfitidea点击:

Convert xml to json with Java

javaxmljson

提问by Dominic

Is there a way to convert a xml file to json? The XML can be of any structure, therefore there is no POJO class for instantiation. I need to convert the xml to json or to a Map, without root nodes.

有没有办法将xml文件转换为json?XML 可以是任何结构,因此没有用于实例化的 POJO 类。我需要将 xml 转换为 json 或 Map,没有根节点。

For example:

例如:

<import name="person">
    <item>
        <firstName>Emil</firstName>
        <lastName>Example</lastName>
        <addresses>
            <address>
                <street>Example Blvd.</street>
            </address>
            <address>
                <street>Example Ave.</street>
            </address>
        </addresses>
    </item>
</import>

Expected JSON

预期的 JSON

{
    "firstName": "Emil",
    "lastName": "Example",
    "addresses": [
        { "street" : "Example Blvd." },
        { "street" : "Example Ave." }
    ]
}

回答by Konstantin.Krivyakin

import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;

XML.toJSONObject(xml_text).toString()

org.json.XML

org.json.XML

回答by Lorenz Pfisterer

On this websiteyou find some helpful classes for your task.

在这个网站上,您可以找到一些对您的任务有用的课程。

public class Main {

    public static int PRETTY_PRINT_INDENT_FACTOR = 4;
    public static String TEST_XML_STRING = "Your xml string here";

    public static void main(String[] args) {
        try {
            JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);
            String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
            System.out.println(jsonPrettyPrintString);
        } catch (JSONException je) {
            System.out.println(je.toString());
        }
    }
}

回答by Adi Sembiring

You can Use JSON and XML Library from json.org

您可以使用来自json.org 的JSON 和 XML 库

import org.json.JSONObject;
import org.json.XML;
import org.junit.Test;

public class XmlToJsonTest {
    private static final String XML_TEXT = "<note>\n" +
            "<to>Tove</to>\n" +
            "<from>Jani</from>\n" +
            "<heading>Reminder</heading>\n" +
            "<body>Don't forget me this weekend!</body>\n" +
            "</note>";
    private static final int PRETTY_PRINT_INDENT_FACTOR = 4;

    @Test
    public void convert() {
        JSONObject xmlJSONObj = XML.toJSONObject(XML_TEXT);
        String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
        System.out.println(jsonPrettyPrintString);
    }
}

Source

来源

<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>

Output

输出

{"note": {
    "heading": "Reminder",
    "from": "Jani",
    "to": "Tove",
    "body": "Don't forget me this weekend!"
}}

回答by tomaj

If you are using Java 8, you should check out my open source library: unXml. unXml basically maps from Xpaths to Json-attributes.

如果您使用的是Java 8,您应该查看我的开源库:unXml。unXml 基本上从 Xpaths 映射到 Json 属性。

It's available on Maven Central.

它在Maven Central上可用。

Example

例子

import com.fasterxml.Hymanson.databind.node.ObjectNode;
import com.nerdforge.unxml.factory.ParsingFactory;
import com.nerdforge.unxml.parsers.Parser;
import org.w3c.dom.Document;

public class ParserExample {
    public ObjectNode parseXml(String xml){
        Parsing parsing = ParsingFactory.getInstance().create();
        Document document = parsing.xml().document(xml);

        Parser<ObjectNode> parser = parsing.obj("//item")
            .attribute("firstName", "firstName")
            .attribute("lastName", "lastName")
            .attribute("addresses", parsing.arr("addresses/address", parsing.obj()
                .attribute("street", "street")
            ))
            .build();

        ObjectNode result = parser.apply(document);
        return result;
    }
}

It will return a HymansonObjectNode, with the json from your example.

它将返回一个HymansonObjectNode,以及您示例中的 json 。

回答by Valentyn Kolesnikov

Underscore-javalibrary has static method U.xmlToJson(xml). I am the maintainer of the project. Live example

Underscore-java库有静态方法U.xmlToJson(xml)。我是项目的维护者。活生生的例子

import com.github.underscore.lodash.U;

public class Main {
    public static void main(String[] args) {
        final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>\n"
            + "  <FirstItem>1</FirstItem>\n  <SecondItem>2</SecondItem>\n</root>";
        System.out.println(U.xmlToJson(xml));
    }
}

回答by Nikunj Paradva

I am used JSON-javabut I found more Efficient Library to Convert XMLto Jsonis XmlToJson

我使用JSON-java的,但我发现更有效的图书馆转换XMLJsonXmlToJson

Which is Given Best Customization to making JsonfromXML

这是考虑最佳的定制,以使得Json来自XML

Add the libarydependency to your APP build.gradlefile

libary依赖项添加到您的 APPbuild.gradle文件中

dependencies {
compile 'com.github.smart-fun:XmlToJson:1.4.4'    // add this line
}


    <?xml version="1.0" encoding="utf-8"?>
    <library>
    <book id="007">James Bond</book>
    <book id="000">Book for the dummies</book>
    </library>
  • Custom Content names

    public String convertXmlToJson(String xml) {
    XmlToJson xmlToJson = new XmlToJson.Builder(xml)
        .setContentName("/library/book", "title")
        .build();
    return xmlToJson.toString();
    }
    

    "library":{  
      "book":[  
         {  
            "id":"007",
            "title":"James Bond"
         },
         {  
            "id":"000",
            "title":"Book for the dummies"
         }
       ]
      }
     }
    
  • 自定义内容名称

    public String convertXmlToJson(String xml) {
    XmlToJson xmlToJson = new XmlToJson.Builder(xml)
        .setContentName("/library/book", "title")
        .build();
    return xmlToJson.toString();
    }
    

    "library":{  
      "book":[  
         {  
            "id":"007",
            "title":"James Bond"
         },
         {  
            "id":"000",
            "title":"Book for the dummies"
         }
       ]
      }
     }
    


  • Custom Attributes names

    public String convertXmlToJson(String xml) {
      XmlToJson xmlToJson = new XmlToJson.Builder(xml)
        .setAttributeName("/library/book/id", "code")
        .build();
      return xmlToJson.toString();
    }
    

    {  
      "library":{  
      "book":[  
         {  
            "code":"007",
            "content":"James Bond"
         },
         {  
            "code":"000",
            "content":"Book for the dummies"
         }
      ]
     }
    }
    
  • 自定义属性名称

    public String convertXmlToJson(String xml) {
      XmlToJson xmlToJson = new XmlToJson.Builder(xml)
        .setAttributeName("/library/book/id", "code")
        .build();
      return xmlToJson.toString();
    }
    

    {  
      "library":{  
      "book":[  
         {  
            "code":"007",
            "content":"James Bond"
         },
         {  
            "code":"000",
            "content":"Book for the dummies"
         }
      ]
     }
    }
    

and Much More Efficient Techniques to Customize your Json

以及更有效的技术来自定义您的 Json

Click here to Check out on Github

单击此处查看 Github

回答by jschnasse

You can use standard tools for it

您可以使用标准工具

  1. Use the tool xjcfrom your jdk to generate Java classes from schema

    Since Java 9you must use explicitly add JAXB as module with –add-modules java.se.eeSee: How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException in Java 9

  1. 使用xjcjdk 中的工具从模式生成 Java 类

    由于Java 9您必须使用显式添加 JAXB 作为模块,–add-modules java.se.ee请参阅:How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException in Java 9

Since Java 11 you have to download xjcin an extra step from https://javaee.github.io/jaxb-v2/

从 Java 11 开始,您必须xjchttps://javaee.github.io/jaxb-v2/额外一步下载

  1. Read in as XMLwrite out as JSONusing Hymanson
  1. 读入如同XML写出一样JSON使用Hymanson

Example

例子

With https://schema.datacite.org/meta/kernel-4.1/metadata.xsd

使用https://schema.datacite.org/meta/kernel-4.1/metadata.xsd

1. Use the tool xjcfrom your jdk

1. 使用xjcjdk 中的工具

In my example I will use a fairly complex example based on datacite schemas.

在我的示例中,我将使用一个基于数据引用模式的相当复杂的示例。

/path/to/jdk/bin/xjc -d /path/to/java/project \
-p stack24174963.datacite \
 https://schema.datacite.org/meta/kernel-4.1/metadata.xsd

This will reply with

这将回复

parsing a schema...
compiling a schema...
stack24174963/datacite/Box.java
stack24174963/datacite/ContributorType.java
stack24174963/datacite/DateType.java
stack24174963/datacite/DescriptionType.java
stack24174963/datacite/FunderIdentifierType.java
stack24174963/datacite/NameType.java
stack24174963/datacite/ObjectFactory.java
stack24174963/datacite/Point.java
stack24174963/datacite/RelatedIdentifierType.java
stack24174963/datacite/RelationType.java
stack24174963/datacite/Resource.java
stack24174963/datacite/ResourceType.java
stack24174963/datacite/TitleType.java
stack24174963/datacite/package-info.java

2. Read in as XMLwrite out as JSONusing Hymanson

2. 读入如同XML写出一样JSON使用 Hymanson

  import com.fasterxml.Hymanson.databind.ObjectMapper;
  import com.fasterxml.Hymanson.databind.SerializationFeature;

  import stack24174963.datacite.Resource;

  public class HowToXmlToJsonWithSchema {
    @Test
    public void readXmlAndConvertToSchema() throws Exception {
        String example = "schemas/datacite/kernel-4.1/example/datacite-example-complicated-v4.1.xml";
        try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(example)) {
            Resource resource = JAXB.unmarshal(in, Resource.class);
            System.out.println(asJson(resource));
        }
    }

    private String asJson(Object obj) throws Exception {
        StringWriter w = new StringWriter();
        new ObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, true).writeValue(w, obj);
        String result = w.toString();
        return result;
    }
  }

Prints:

印刷:

 {
      "identifier" : {
        "value" : "10.5072/testpub",
        "identifierType" : "DOI"
      },
      "creators" : {
        "creator" : [ {
          "creatorName" : {
            "value" : "Smith, John",
            "nameType" : "PERSONAL"
          },
          "givenName" : "<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n<givenName xmlns=\"http://datacite.org/schema/kernel-4\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">John</givenName>",
          "familyName" : "<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n<familyName xmlns=\"http://datacite.org/schema/kernel-4\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">Smith</familyName>",
          "nameIdentifier" : [ ],
          "affiliation" : [ ]
        }, {
          "creatorName" : {
            "value" : "つまらないものですが",
            "nameType" : null
          },
          "givenName" : null,
          "familyName" : null,
          "nameIdentifier" : [ {
            "value" : "0000000134596520",
            "nameIdentifierScheme" : "ISNI",
            "schemeURI" : "http://isni.org/isni/"
          } ],
          "affiliation" : [ ]
        } ]
      },
      "titles" : {
        "title" : [ {
          "value" : "W?a?ciwo?ci rzutowań podprzestrzeniowych",
          "titleType" : null,
          "lang" : "pl"
        }, {
          "value" : "Translation of Polish titles",
          "titleType" : "TRANSLATED_TITLE",
          "lang" : "en"
        } ]
      },
      "publisher" : "Springer",
      "publicationYear" : "2010",
      "resourceType" : {
        "value" : "Monograph",
        "resourceTypeGeneral" : "TEXT"
      },
      "subjects" : {
        "subject" : [ {
          "value" : "830 German & related literatures",
          "subjectScheme" : "DDC",
          "schemeURI" : null,
          "valueURI" : null,
          "lang" : "en"
        }, {
          "value" : "Polish Literature",
          "subjectScheme" : null,
          "schemeURI" : null,
          "valueURI" : null,
          "lang" : "en"
        } ]
      },
      "contributors" : {
        "contributor" : [ {
          "contributorName" : {
            "value" : "Doe, John",
            "nameType" : "PERSONAL"
          },
          "givenName" : "<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n<givenName xmlns=\"http://datacite.org/schema/kernel-4\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">John</givenName>",
          "familyName" : "<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n<familyName xmlns=\"http://datacite.org/schema/kernel-4\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">Doe</familyName>",
          "nameIdentifier" : [ {
            "value" : "0000-0001-5393-1421",
            "nameIdentifierScheme" : "ORCID",
            "schemeURI" : "http://orcid.org/"
          } ],
          "affiliation" : [ ],
          "contributorType" : "DATA_COLLECTOR"
        } ]
      },
      "dates" : null,
      "language" : "de",
      "alternateIdentifiers" : {
        "alternateIdentifier" : [ {
          "value" : "937-0-4523-12357-6",
          "alternateIdentifierType" : "ISBN"
        } ]
      },
      "relatedIdentifiers" : {
        "relatedIdentifier" : [ {
          "value" : "10.5272/oldertestpub",
          "resourceTypeGeneral" : null,
          "relatedIdentifierType" : "DOI",
          "relationType" : "IS_PART_OF",
          "relatedMetadataScheme" : null,
          "schemeURI" : null,
          "schemeType" : null
        } ]
      },
      "sizes" : {
        "size" : [ "256 pages" ]
      },
      "formats" : {
        "format" : [ "pdf" ]
      },
      "version" : "2",
      "rightsList" : {
        "rights" : [ {
          "value" : "Creative Commons Attribution-NoDerivs 2.0 Generic",
          "rightsURI" : "http://creativecommons.org/licenses/by-nd/2.0/",
          "lang" : null
        } ]
      },
      "descriptions" : {
        "description" : [ {
          "content" : [ "\n      Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea\n      takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores\n      et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.\n    " ],
          "descriptionType" : "ABSTRACT",
          "lang" : "la"
        } ]
      },
      "geoLocations" : null,
      "fundingReferences" : null
    }

For example input XML :

例如输入 XML :

  <?xml version="1.0" encoding="UTF-8"?>
  <resource xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://datacite.org/schema/kernel-4" xsi:schemaLocation="http://datacite.org/schema/kernel-4 http://schema.datacite.org/meta/kernel-4.1/metadata.xsd">
    <identifier identifierType="DOI">10.5072/testpub</identifier>
    <creators>
      <creator>
        <creatorName nameType="Personal">Smith, John</creatorName>
        <givenName>John</givenName>
        <familyName>Smith</familyName>
      </creator>
      <creator>
        <creatorName>つまらないものですが</creatorName>
        <nameIdentifier nameIdentifierScheme="ISNI" schemeURI="http://isni.org/isni/">0000000134596520</nameIdentifier>
      </creator>
    </creators>
    <titles>
      <title xml:lang="pl">W?a?ciwo?ci rzutowań podprzestrzeniowych</title>
      <title xml:lang="en" titleType="TranslatedTitle">Translation of Polish titles</title>
    </titles>
    <publisher>Springer</publisher>
    <publicationYear>2010</publicationYear>
    <subjects>
      <subject xml:lang="en" subjectScheme="DDC">830 German &amp; related literatures</subject>
      <subject xml:lang="en">Polish Literature</subject>
    </subjects>
    <contributors>
      <contributor contributorType="DataCollector">
        <contributorName nameType="Personal">Doe, John</contributorName>
        <givenName>John</givenName>
        <familyName>Doe</familyName>
        <nameIdentifier nameIdentifierScheme="ORCID" schemeURI="http://orcid.org/">0000-0001-5393-1421</nameIdentifier>
      </contributor>
    </contributors>
    <language>de</language>
    <resourceType resourceTypeGeneral="Text">Monograph</resourceType>
    <alternateIdentifiers>
      <alternateIdentifier alternateIdentifierType="ISBN">937-0-4523-12357-6</alternateIdentifier>
    </alternateIdentifiers>
    <relatedIdentifiers>
      <relatedIdentifier relatedIdentifierType="DOI" relationType="IsPartOf">10.5272/oldertestpub</relatedIdentifier>
    </relatedIdentifiers>
    <sizes>
      <size>256 pages</size>
    </sizes>
    <formats>
      <format>pdf</format>
    </formats>
    <version>2</version>
    <rightsList>
      <rights rightsURI="http://creativecommons.org/licenses/by-nd/2.0/">Creative Commons Attribution-NoDerivs 2.0 Generic</rights>
    </rightsList>
    <descriptions>
      <description xml:lang="la" descriptionType="Abstract">
        Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea
        takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores
        et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
      </description>
    </descriptions>
  </resource>