0
다음 코드가 있습니다. 아이디어는 XML 문자열을 Json으로 변환하고 데이터베이스에 저장하며 (JSON이어야 함) 원본 문자열을 검색하여 원래 형식으로 표시합니다. 문제는 이전xml을 json으로 변환하고 xml 반환 오류로 돌아 가기
내 코드
String xml = "<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:typ=\"http://org.typ.com/resto\">\r\n"
+ " <soapenv:Header/>\r\n"
+ " <soapenv:Body>\r\n"
+ " <typ:MY-OPERATION>\r\n"
+ " <RequestCall>\r\n"
+ " <clientIp>0.0.0.0</clientIp>\r\n"
+ " <data xsi:nil=\"true\" />\r\n"
+ " </RequestCall>\r\n"
+ " </typ:MY-OPERATION>\r\n" + " </soapenv:Body>\r\n" + "</soapenv:Envelope>";
XMLSerializer serializer = new XMLSerializer();
JSON jsonObject = serializer.read(xml);
String jsonString = jsonObject.toString();
System.out.println(jsonString);
serializer.setTypeHintsEnabled(false);
JSON jsonObject2 = JSONSerializer.toJSON(jsonString);
String xmlOut = serializer.write(jsonObject2);
System.out.println(xmlOut);
출력은 JSON의 다음 (매우 이상한 형태입니다 JSON으로 XML을 변환 같은 라이브러리를 사용의 난에도 불구하고 XML로 JSON 변환하지 못할 것입니다)
04/04/2014 17:44:50 net.sf.json.xml.XMLSerializer getType
INFO: Using default type string
{"@xmlns:soapenv":"http://schemas.xmlsoap.org/soap/envelope/","@xmlns:typ":"http://org.typ.com/resto","@xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance","soapenv:Header":null,"soapenv:Body":{"typ:MY-OPERATION":{"@xmlns:typ":"http://org.typ.com/resto","RequestCall":{"clientIp":"0.0.0.0","data":{"@xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance","@xsi:nil":"true"}}}}}
Exception in thread "main" nu.xom.NamespaceConflictException: Attribute prefixes must be declared.
at nu.xom.Attribute._setNamespace(Unknown Source)
at nu.xom.Attribute.<init>(Unknown Source)
at nu.xom.Attribute.<init>(Unknown Source)
at net.sf.json.xml.XMLSerializer.processJSONObject(XMLSerializer.java:962)
at net.sf.json.xml.XMLSerializer.processJSONValue(XMLSerializer.java:1040)
at net.sf.json.xml.XMLSerializer.processJSONObject(XMLSerializer.java:990)
at net.sf.json.xml.XMLSerializer.processJSONValue(XMLSerializer.java:1040)
at net.sf.json.xml.XMLSerializer.processJSONObject(XMLSerializer.java:990)
at net.sf.json.xml.XMLSerializer.processJSONValue(XMLSerializer.java:1040)
at net.sf.json.xml.XMLSerializer.processJSONObject(XMLSerializer.java:990)
at net.sf.json.xml.XMLSerializer.processJSONValue(XMLSerializer.java:1040)
at net.sf.json.xml.XMLSerializer.processJSONObject(XMLSerializer.java:990)
at net.sf.json.xml.XMLSerializer.write(XMLSerializer.java:605)
at net.sf.json.xml.XMLSerializer.write(XMLSerializer.java:570)
at com.monguito.MongoService.main(MongoService.java:92)
_ "json의 형태가 매우 이상합니다."_ 바로 그 풍자입니다. –
제 말은 라이브러리가 유형에 대한 참조가있는 각 xml 필드의 json 문자열에 네임 스페이스를 추가한다는 것입니다. 예를 들어 xml xsi : nill 내부에서 사용하는 경우. json 출력은 완전한 네임 스페이스를 추가합니다. 아무 것도 보지 않고 댓글을 달았거나 알 수 있습니까? – user1655510
스레드 "main"... _ 등의 오류가 있습니다. 이미 알고 있을지 모르지만 명확하지 않습니다. –