5

在我的 XML 文件中,我有一些实体,例如’

所以我为我的 XML 文档创建了一个 DTD 标记来定义这些实体。下面是用于读取 XML 文件的 Java 代码。

SAXBuilder builder = new SAXBuilder();
URL url = new URL("http://127.0.0.1:8080/sample/subject.xml");        
InputStream stream = url.openStream();
org.jdom.Document document = builder.build(stream);

Element root = document.getRootElement();

Element name = root.getChild("name");
result = name.getText();
System.err.println(result);

如何更改 Java 代码以通过 HTTP 检索 DTD,以使我的 XML 文档的解析没有错误?

xml 文档的简化示例。


<main>
  <name>hello &lsquo; world &rsquo; foo  &amp; bar </name> 
</main>
4

1 回答 1

3

一种方法是阅读文档,然后使用转换器对其进行验证:

import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class ValidateWithExternalDTD {
    private static final String URL = "http://127.0.0.1:8080/sample/subject.xml";
    private static final String DTD = "http://127.0.0.1/YourDTD.dtd";

    public static void main(String args[]) {
        try {
            DocumentBuilderFactory factory=                   DocumentBuilderFactory.newInstance();
            factory.setValidating(true);
            DocumentBuilder builder = factory.newDocumentBuilder();

            // Set the error handler
            builder.setErrorHandler(new org.xml.sax.ErrorHandler() {                
                public void fatalError(SAXParseException spex)
                        throws SAXException {
                    // output error and exit
                    spex.printStackTrace();
                    System.exit(0);
                }

                public void error(SAXParseException spex)
                        throws SAXParseException {
                    // output error and continue
                    spex.printStackTrace();
                }

                public void warning(SAXParseException spex)
                        throws SAXParseException {
                    // output warning and continue
                    spex.printStackTrace();
                }
            });

            // Read the document
            URL url = new URL(ValidateWithExternalDTD.URL);
            Document xmlDocument = builder.parse(url.openStream());
            DOMSource source = new DOMSource(xmlDocument);

            // Use the tranformer to validate the document
            StreamResult result = new StreamResult(System.out);                     
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, ValidateWithExternalDTD.DTD);
            transformer.transform(source, result);

            // Process your document if everything is OK
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

另一种方法是将 XML 标题替换为 XML 标题加上 DTD 引用

替换这个:

<?xml version = "1.0"?>

有了这个:

<?xml version = "1.0"?><!DOCTYPE ...>

当然,您只会替换第一次出现,而不是尝试浏览整个 xml 文档

您必须通过将 true(validate) 传递给其构造函数来实例化 SAXBuilder:

SAXBuilder builder = new SAXBuilder(true);

或致电:

builder.setValidation(true)
于 2011-02-04T12:22:22.537 回答