错误:引起:com.sun.istack.internal.saxexception2:在这种情况下找不到的java.u



我想将XML转换为JSON,在使用DTD模式返回有效XML之后。

我有返回JSONOBJECT的方法:

public JSONObject xml2JSON(InputStream xml) throws IOException, JDOMException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        while ((len = xml.read(buffer)) > -1 ) {
            baos.write(buffer, 0, len);
        }
        baos.flush();
        InputStream is1 = new ByteArrayInputStream(baos.toByteArray());
        InputStream is2 = new ByteArrayInputStream(baos.toByteArray());
        String s = input2String(is1);
        if(validationDTD(is2)) {
            return XML.toJSONObject(s);
        }
        return null;
    }
public Boolean validationDTD(InputStream xml) throws JDOMException, IOException {
    try {
        SAXBuilder builder = new SAXBuilder(XMLReaders.DTDVALIDATING);
        Document validDocument = builder.build(xml);
        validDocument.getDocType();
        return true;
    } catch (JDOMException e) {
        return false;
    } catch (IOException e) {
        return false;
    }
}
public String input2String(InputStream inputStream) throws IOException {
    return IOUtils.toString(inputStream, Charset.defaultCharset());
}

和返回正确xml的方法:

public String JSONtoXML(JSONObject jsonObject) {
    String finalString = DOCTYPE.concat(XML.toString(jsonObject));
    return finalString;
}

具有用于添加DTD的变量:

private static final String DOCTYPE = "<?xml version="1.0" encoding="UTF-8"?>n" +
        "<!DOCTYPE ep-request SYSTEM "myDtd.dtd">";

我有此测试:

@Test
public void xml2JSONShouldReturnString() throws IOException, JDOMException {
    InputStream xmlInputString = this.getClass().getClassLoader().getResourceAsStream("myXmlDtd.xml");
    service.xml2JSON(xmlInputString);
}
@Test
public void validateDTDShouldReturnDocument() throws IOException, JDOMException {
    InputStream xmlInputString = this.getClass().getClassLoader().getResourceAsStream("myXmlDtd.xml");
    Assert.assertEquals(true, service.validationDTD(xmlInputString));
}
@Test
public void JSON2toxmlShouldReturnValidXML() throws IOException, JDOMException {
    InputStream xmlInputString = this.getClass().getClassLoader().getResourceAsStream("myXmlDtd.xml");
    JSONObject jsonObject = service.xml2JSON(xmlInputString);
    String xmlOut = eblService.JSONtoXML(jsonObject);
    Assert.assertEquals(true, service.validationDTD(new ByteArrayInputStream(xmlOut.getBytes())));
}

但是最后一个失败了,因为xml的dtd格式不正确。

如何制作有效的XML(与DTD匹配(?

编辑:现在,我将XML解析到PoJo(由XJC -DTD MyDtd.dtd生成(和Pojo和Json和Viceversa。但是我在POJO遇到XML序列化时遇到麻烦,因为我的Pojo包含:

@XmlElements({
    @XmlElement(name = "file-reference-id", required = true, type = 
FileReferenceId.class),
     @XmlElement(name = "request-petition", required = true, type = 
    RequestPetition.class)
    })
protected List<Object> fileRefenceIdOrRequestPetition;

当我的pojo包含linkedhashmap的列表并返回linkedhashmap不在jaxbcontext中时,问题出现了,但是如果我将类的类型更改为linkedhashmap.class.class。它包含在linkedhashmap.class

有许多不同的库将JSON转换为XML,它们都会产生不同的答案,具有不同的优势和劣势。(例如,它们对处理无效XML名称的JSON键的问题都有不同的解决方案。(通常,它们对XML的格式没有太大的控制权,这意味着您通常必须转换生成了您实际想要的格式的XML,例如带有XSLT样式表。如果您有XML必须符合的特定DTD,那肯定是这种情况。

注意:XSLT 3.0中的json-to-xml()函数会产生直接反映JSON语法的XML,并具有

之类的构造
<map>
  <string key="first">John</string>
  <string key="last">Smith</string>
</map>

这里的想法是,用户将始终想要将其转换为所需的目标格式,并且由于您已经在XSLT中,因此此转换毫无问题。

最新更新