如何将 XML 转换为具有二进制数据内容的 Json
Posted
技术标签:
【中文标题】如何将 XML 转换为具有二进制数据内容的 Json【英文标题】:How to convert XML to Json with binary data content 【发布时间】:2021-04-13 21:37:43 【问题描述】:我尝试从 xml 转换为 json。它适用于普通的文本数据内容。当 xml 标签中的数据是二进制时,我们无法将 xml 解组为 java 对象。您能帮忙分享一下我们如何将 xml 转换为 json 的二进制文件吗?
Java 代码:
public <T> String xmlToJson(Class<T> clazz, String xmlString, boolean includeRoot) throws CustomException
StringWriter writer = new StringWriter();
String jsonString = "";
try
XMLInputFactory xif = XMLInputFactory.newInstance();
xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
xif.setProperty(XMLInputFactory.SUPPORT_DTD, true);
XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xmlString));
JAXBContext jc = JAXBContext.newInstance(clazz);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty("eclipselink.media-type", "application/json");
marshaller.setProperty("eclipselink.json.include-root", includeRoot);
marshaller.setProperty(MarshallerProperties.JSON_ATTRIBUTE_PREFIX, "@");
JAXBElement<T> addressFromXML = unmarshaller.unmarshal(xsr, clazz); //ERROR at this line for binary data
marshaller.marshal(addressFromXML, writer);
jsonString = writer.toString();
catch (Exception e)
logger.error(e.getMessage());
throw new CustomException(e.getMessage());
return jsonString;
这是错误:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: javax/mail/internet/MimeMultipart] with root cause
java.lang.NoClassDefFoundError: javax/mail/internet/MimeMultipart
at org.eclipse.persistence.internal.oxm.XMLBinaryDataHelper.initializeDataTypes(XMLBinaryDataHelper.java:74)
at org.eclipse.persistence.internal.oxm.XMLBinaryDataHelper.<init>(XMLBinaryDataHelper.java:54)
at org.eclipse.persistence.internal.oxm.XMLBinaryDataHelper.getXMLBinaryDataHelper(XMLBinaryDataHelper.java:60)
at org.eclipse.persistence.internal.oxm.XMLInlineBinaryHandler.endElement(XMLInlineBinaryHandler.java:126)
at org.eclipse.persistence.internal.oxm.record.deferred.EndElementEvent.processEvent(EndElementEvent.java:37)
at org.eclipse.persistence.internal.oxm.record.deferred.DeferredContentHandler.executeEvents(DeferredContentHandler.java:64)
at org.eclipse.persistence.internal.oxm.record.deferred.BinaryMappingContentHandler.executeEvents(BinaryMappingContentHandler.java:75)
at org.eclipse.persistence.internal.oxm.record.deferred.BinaryMappingContentHandler.processSimpleElement(BinaryMappingContentHandler.java:67)
at org.eclipse.persistence.internal.oxm.record.deferred.DeferredContentHandler.endElement(DeferredContentHandler.java:122)
at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parseEvent(XMLStreamReaderReader.java:150)
at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parse(XMLStreamReaderReader.java:100)
at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parse(XMLStreamReaderReader.java:87)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:1016)
at org.eclipse.persistence.internal.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:657)
at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:460)
【问题讨论】:
But it fail for binary data content
- 错误在哪里?如果您不在 try/catch 块中吞下异常,这将有所帮助...
嗨@rkosegi,我刚刚更新了我的问题,其中包括堆栈跟踪。这是我第一次尝试将带有二进制内容的 xml 解析为对象以及从对象解析为 json。
java.lang.NoClassDefFoundError: javax/mail/internet/MimeMultipart
您的类路径中缺少 java mail-api。
如果您使用的是 maven,您应该能够通过添加流动依赖项来解决这个问题:
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
</dependency>
更多关于Java Mail API的信息
【讨论】:
感谢 rkosegi。它运作良好。我仍然不明白为什么 javax.mail 会涉及从 xml 到对象的解组。你能帮忙解释一下吗? 添加库后以上是关于如何将 XML 转换为具有二进制数据内容的 Json的主要内容,如果未能解决你的问题,请参考以下文章
Spark:如何从具有属性的多个嵌套 XML 文件转换为 Data Frame 数据
将XML转换为Json(标记为UTF-16但具有UTF-8内容的文档)