在java中解析xml时忽略DTD

时间:2013-07-29 07:19:11

标签: java xml-parsing doctype

我正在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");

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

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

秘诀是提供一个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");
    }

您可以阅读this链接。