0

我正在处理以下xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE CASE SYSTEM "C:\Users\user123\Desktop\demo.dtd">
<INFO>
  <NAME>John Smith</NAME>
  <BUSINESSNAME>Smith</BUSINESSNAME>
</INFO>

但我不得不忽略

<!DOCTYPE CASE SYSTEM "C:\Users\user123\Desktop\demo.dtd"> because in presence of this parsing is giving error.

这是我的代码:

        StringBuilder sb = new StringBuilder();
        File xmlFile = new File("C:/Users/Desktop/demo.xml");

        BufferedReader br = new BufferedReader(new FileReader(xmlFile));
        String line = null;
        while((line = br.readLine())!= null){
            if(line.indexOf("&") != -1)
            {
                line = line.replaceAll("&","&amp;");
            }
                sb.append(line);
        }
        br.close();

        BufferedWriter bw = new BufferedWriter(new FileWriter(xmlFile));

        Source xmlInput = new StreamSource(new StringReader(sb.toString()));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", 2);
        Transformer transformer = transformerFactory.newTransformer(); 
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);


        bw.write(xmlOutput.getWriter().toString());
        bw.close();
        System.out.println("success");

有什么建议么?

4

1 回答 1

0

一种方法显然是从输入 xml 中删除它,但这是一个 hack,还有另一种方法可以让您删除它或从不同的位置加载它。

秘诀是提供一个 XML 阅读器并指定一个实体解析器,它返回所需的 dtd 内容(一个空白字符串)。

这是代码:

public static void main(String[] args) throws IOException {
        StringBuilder sb = new StringBuilder();
        String path= "C:/Users/.../Desktop/demo.xml";
        File xmlFile = new File(path);
        String path2= "C:/Users/.../Desktop/demo2.xml";
        File xmlFile2 = new File(path2);

        BufferedReader br = new BufferedReader(new FileReader(xmlFile));
        String line = null;
        while((line = br.readLine())!= null){
            if(line.indexOf("&") != -1)
            {
                line = line.replaceAll("&","&amp;");
            }
            sb.append(line);
        }
        br.close();

        BufferedWriter bw = new BufferedWriter(new FileWriter(xmlFile2));

        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", 2);
        Transformer transformer = null;
        try {
            transformer = transformerFactory.newTransformer();
            System.out.println(transformer.getOutputProperties());
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        try {
            XMLReader reader = XMLReaderFactory.createXMLReader();
            reader.setEntityResolver(new EntityResolver() {

                @Override
                public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                    if (systemId.endsWith(".dtd")) {
                        StringReader stringInput = new StringReader(" ");
                        return new InputSource(stringInput);
                    }
                    else {
                        return null; // use default behavior
                    }
                }
            });
            SAXSource xmlSource = new SAXSource(reader, new InputSource(new StringReader(sb.toString())));
            transformer.transform(xmlSource, xmlOutput);
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }


        bw.write(xmlOutput.getWriter().toString());
        bw.close();
        System.out.println("success");
    }

你可以阅读这个链接。

于 2013-07-29T08:59:04.187 回答