programing

Java에서 JSON을 XML로 변환

goodjava 2023. 2. 15. 22:03

Java에서 JSON을 XML로 변환

저는 json이 처음입니다.json 객체에서 xml을 생성하는 프로그램을 가지고 있습니다.

String str = "{'name':'JSON','integer':1,'double':2.0,'boolean':true,'nested':{'id':42},'array':[1,2,3]}";  
    JSON json = JSONSerializer.toJSON( str );  
    XMLSerializer xmlSerializer = new XMLSerializer();  
    xmlSerializer.setTypeHintsCompatibility( false );  
    String xml = xmlSerializer.write( json );  
    System.out.println(xml); 

출력은 다음과 같습니다.

<?xml version="1.0" encoding="UTF-8"?>
<o><array json_class="array"><e json_type="number">1</e><e json_type="number">2</e><e json_type="number">3</e></array><boolean json_type="boolean">true</boolean><double json_type="number">2.0</double><integer json_type="number">1</integer><name json_type="string">JSON</name><nested json_class="object"><id json_type="number">42</id></nested></o>

가장 큰 문제는 json_type="number" 대신 나만의 속성을 어떻게 쓰느냐와 같은 나만의 하위 요소를 쓰는 것입니다.

다음에, json.org 의 JSON-Java 라이브러리를 사용합니다.

JSONObject json = new JSONObject(str);
String xml = XML.toString(json);

toString는 두 번째 인수를 사용하여 XML 루트노드의 이름을 지정할 수 있습니다.

또한 이 라이브러리는 다음을 사용하여 XML을 JSON으로 변환할 수 있습니다.XML.toJSONObject(java.lang.String string)

자바독 체크

github 저장소에 대한 링크

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20160212</version>
</dependency>

새로운 링크로 갱신된 원래의 투고

Underscore-java 라이브러리에는 정적 메서드가 있습니다.U.jsonToXml(jsonstring)실제

import com.github.underscore.U;

public class MyClass {
    public static void main(String args[]) {
        String json = "{\"name\":\"JSON\",\"integer\":1,\"double\":2.0,\"boolean\":true,\"nested\":{\"id\":42},\"array\":[1,2,3]}";  
        System.out.println(json); 
        String xml = U.jsonToXml(json);  
        System.out.println(xml); 
    }
}

출력:

{"name":"JSON","integer":1,"double":2.0,"boolean":true,"nested":{"id":42},"array":[1,2,3]}
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <name>JSON</name>
  <integer number="true">1</integer>
  <double number="true">2.0</double>
  <boolean boolean="true">true</boolean>
  <nested>
    <id number="true">42</id>
  </nested>
  <array number="true">1</array>
  <array number="true">2</array>
  <array number="true">3</array>
</root>

json to xml의 경우 다음 잭슨 예를 사용합니다.

final String str = "{\"name\":\"JSON\",\"integer\":1,\"double\":2.0,\"boolean\":true,\"nested\":{\"id\":42},\"array\":[1,2,3]}";
ObjectMapper jsonMapper = new ObjectMapper();
JsonNode node = jsonMapper.readValue(str, JsonNode.class);
XmlMapper xmlMapper = new XmlMapper();
                xmlMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
                xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
                xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_1_1, true);
ObjectWriter ow = xmlMapper.writer().withRootName("root");
StringWriter w = new StringWriter();
ow.writeValue(w, node);
System.out.println(w.toString());

인쇄:

<?xml version='1.1' encoding='UTF-8'?>
<root>
  <name>JSON</name>
  <integer>1</integer>
  <double>2.0</double>
  <boolean>true</boolean>
  <nested>
    <id>42</id>
  </nested>
  <array>1</array>
  <array>2</array>
  <array>3</array>
</root>

(xml에서 json으로) 다시 변환하려면 https://stackoverflow.com/a/62468955/1485527 를 참조하십시오.

xml에 유효한 dtd 파일이 있는 경우 ecclipselink jar 바이너리를 사용하여 json을 xml로, xml을 json으로 쉽게 변환할 수 있습니다.

http://www.cubicrace.com/2015/06/How-to-convert-XML-to-JSON-format.html 를 참조해 주세요.

이 문서에는 참조용으로 다운로드할 수 있는 zip 파일로 샘플 프로젝트(지원하는 서드파티제 항아리 포함)도 포함되어 있습니다.

XSLT 3.0을 사용한 변환만이 적절한 방법이라고 생각합니다.유효한 XML을 생성할 수 있으며, 그에 적합한 구조를 생성할 수 있습니다.https://www.w3.org/TR/xslt/ #json

노드 값을 바꾸려면 다음과 같이 하십시오.

JSONObject json = new JSONObject(str);
String xml = XML.toString(json);
xml.replace("old value", "new value");

언급URL : https://stackoverflow.com/questions/19977979/converting-json-to-xml-in-java