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." }
]
}
7 个解决方案
#1
12
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
XML.toJSONObject(xml_text).toString()
org.json.XML
#2
8
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!"
}}
#3
3
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基本上从Xpath映射到Json属性。
It's available on Maven Central.
它可以在Maven Central上找到。
Example
例
import com.fasterxml.jackson.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 Jackson ObjectNode
, with the json from your example.
它将返回一个Jackson ObjectNode,其中包含你的例子中的json。
#4
1
On this website you 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());
}
}
}
#5
1
I am used JSON-java but I found more Efficient Library to Convert XML
to Json
is XmlToJson
我使用的是JSON-java,但我发现更多Efficient Library将XML转换为Json是XmlToJson
Which is Given Best Customization to making Json
fromXML
这是从JML制作Json的最佳定制
Add the libary
dependency to your APP build.gradle
file
将libary依赖项添加到APP build.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”) 。建立(); return xmlToJson.toString(); } “图书馆”:{ “书”:[ { “ID”: “007”, “头衔”:“詹姆斯邦德” }, { “ID”: “000”, “标题”:“预定傻瓜” } ] } }
-
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”) 。建立(); return xmlToJson.toString(); } { “图书馆”:{ “书”:[ { “代码”: “007”, “内容”:“詹姆斯邦德” }, { “代码”: “000”, “内容”:“预定傻瓜” } ] } }
and Much More Efficient Techniques to Customize your Json
和更有效的技术来定制你的Json
Click here to Check out on Github
点击这里查看Github
#6
0
You can use standard tools for it
您可以使用标准工具
- Use the tool
xjc
from your jdk to generate Java classes from schemaSince
Java 9
you must use explicitly add JAXB as module with–add-modules java.se.ee
从Java 9开始,您必须使用-add-modules java.se.ee显式添加JAXB作为模块
- Read in as
XML
write out asJSON
using Jackson - 使用Jackson以XML格式读写为JSON
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 xjc
from your jdk
1.使用jdk中的工具xjc
In my example I will use a fairly complex example based on datacite schemas.
在我的示例中,我将使用基于datacite模式的相当复杂的示例。
/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 XML
write out as JSON
using Jackson
2.使用Jackson以XML格式读写为JSON
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.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 & 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>
#7
0
There is underscore-java library with static methods fromXml and toJson. Live example.
有一些带有来自XML和toJson的静态方法的下划线-java库。实例。
Code example:
代码示例:
import com.github.underscore.lodash.U;
import java.util.Map;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class StringTest {
@SuppressWarnings("unchecked")
@Test
public void toXmlFromJson() {
final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>\n"
+ " <FirstItem>1</FirstItem>\n <SecondItem>2</SecondItem>\n</root>";
assertEquals("{\n"
+ " \"root\": {\n"
+ " \"FirstItem\": \"1\",\n"
+ " \"SecondItem\": \"2\"\n"
+ " }\n"
+ "}",
U.toJson((Map<String, Object>) U.fromXml(xml)));
}
}
#1
12
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
XML.toJSONObject(xml_text).toString()
org.json.XML
#2
8
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!"
}}
#3
3
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基本上从Xpath映射到Json属性。
It's available on Maven Central.
它可以在Maven Central上找到。
Example
例
import com.fasterxml.jackson.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 Jackson ObjectNode
, with the json from your example.
它将返回一个Jackson ObjectNode,其中包含你的例子中的json。
#4
1
On this website you 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());
}
}
}
#5
1
I am used JSON-java but I found more Efficient Library to Convert XML
to Json
is XmlToJson
我使用的是JSON-java,但我发现更多Efficient Library将XML转换为Json是XmlToJson
Which is Given Best Customization to making Json
fromXML
这是从JML制作Json的最佳定制
Add the libary
dependency to your APP build.gradle
file
将libary依赖项添加到APP build.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”) 。建立(); return xmlToJson.toString(); } “图书馆”:{ “书”:[ { “ID”: “007”, “头衔”:“詹姆斯邦德” }, { “ID”: “000”, “标题”:“预定傻瓜” } ] } }
-
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”) 。建立(); return xmlToJson.toString(); } { “图书馆”:{ “书”:[ { “代码”: “007”, “内容”:“詹姆斯邦德” }, { “代码”: “000”, “内容”:“预定傻瓜” } ] } }
and Much More Efficient Techniques to Customize your Json
和更有效的技术来定制你的Json
Click here to Check out on Github
点击这里查看Github
#6
0
You can use standard tools for it
您可以使用标准工具
- Use the tool
xjc
from your jdk to generate Java classes from schemaSince
Java 9
you must use explicitly add JAXB as module with–add-modules java.se.ee
从Java 9开始,您必须使用-add-modules java.se.ee显式添加JAXB作为模块
- Read in as
XML
write out asJSON
using Jackson - 使用Jackson以XML格式读写为JSON
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 xjc
from your jdk
1.使用jdk中的工具xjc
In my example I will use a fairly complex example based on datacite schemas.
在我的示例中,我将使用基于datacite模式的相当复杂的示例。
/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 XML
write out as JSON
using Jackson
2.使用Jackson以XML格式读写为JSON
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.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 & 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>
#7
0
There is underscore-java library with static methods fromXml and toJson. Live example.
有一些带有来自XML和toJson的静态方法的下划线-java库。实例。
Code example:
代码示例:
import com.github.underscore.lodash.U;
import java.util.Map;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class StringTest {
@SuppressWarnings("unchecked")
@Test
public void toXmlFromJson() {
final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>\n"
+ " <FirstItem>1</FirstItem>\n <SecondItem>2</SecondItem>\n</root>";
assertEquals("{\n"
+ " \"root\": {\n"
+ " \"FirstItem\": \"1\",\n"
+ " \"SecondItem\": \"2\"\n"
+ " }\n"
+ "}",
U.toJson((Map<String, Object>) U.fromXml(xml)));
}
}