[英]Adding reference entity to parse an XML file in JAVA
我正在这样读取JAVA中的XML文件:
String filepath = xmlFile;
File workFile = new File(filepath);
File fragmentDir = workFile.getParentFile();
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filepath);
在这个xml文件中,有&testRef;之类的引用。
我在另一个文件中声明了所有这样的实体:
<!ENTITY testRef 'hello"'>
简单的解决方案是直接在xml文件中添加这些引用,或者添加!ENTITY系统“ file.ref”,但我不能。 有没有可以告诉我的DocumentBuilderFactory的解决方案:使用此文件来读取引用?
编辑:我已经尝试过:
docBuilder.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) throws SAXException,IOException {
System.out.println("Resolving");
return new InputSource("file.ref");
}
});
但是我一直在获取“已引用实体“ testRef”,但未声明...”,甚至未打印文本“ Resolving”。 似乎文档生成器没有考虑新的解析器。
我想念什么?
使用DocumentBuilder
的setEntityResolver
方法。
class Resolver implements EntityResolver {
public InputSource resolveEntity(String publicId, String systemId) {
if (systemId.equals("THE_SYSTEM_ID_THAT_YOU_ARE_USING")) {
System.out.println("Resolving Entity...");
return new InputSource("YOUR_REFERENCES_XML");
} else {
// use the default behaviour
return null;
}
}
}
接着
DocumentBuilder builder = factory.newDocumentBuilder();
Resolver res = new Resolver();
builder.setEntityResolver(res);
Document doc = builder.parse("YOUR XML FILE");
编辑:
在深入研究EntityResolver之后,我看到如果XML文件未声明Entity Refernce文件(这是您面临的问题),则忽略实体解析器。 但是,您可以采用以下技巧使其起作用:
static String template =
"<!DOCTYPE x [ "
+ "<!ENTITY % entities SYSTEM \"{0}\"> "
+ "<!ENTITY file SYSTEM \"{1}\" >" + "%entities;" + "]>"
+ "<x>&file;</x>";
private static String createFromTemplate(File entityFile, File xmlFile) {
return MessageFormat.format(template, entityFile.getAbsolutePath(),
xmlFile.getAbsolutePath());
}
private static File entityFile = new File(<ENTITYFILE>);
public Document parse(File f) throws JDOMException, IOException {
String xml = createFromTemplate(entityFile, f);
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new StringReader(xml));
Element e = (Element) doc.getRootElement().getChildren().get(0);
e.detach();
Document doc2 = new Document(e);
return doc2;
}
ENTITYFILE是声明实体引用的文件。
希望能有所帮助。
public static void main(String[] args) {
try {
System.out.println("Start Process");
File xmlFile = new File("/Users/cgonzalez/Desktop/test.xml");
SAXBuilder builder = new SAXBuilder();
Document document = new Document();
document = builder.build(xmlFile);
HashMap piMap = new HashMap(2);
piMap.put("type", "text/xsl");
piMap.put("href", "http://localhost:8080/achs-expdf-web/app/resource/xslt/plantilla.xsl");
ProcessingInstruction pi = new ProcessingInstruction("xml-stylesheet", piMap);
document.getContent().add(0, pi);
// new XMLOutputter().output(doc, System.out);
XMLOutputter xmlOutput = new XMLOutputter();
// display nice nice
xmlOutput.setFormat(Format.getPrettyFormat());
xmlOutput.output(document, new FileWriter("/Users/cgonzalez/Desktop/test.xml"));
System.out.println("end Process");
} catch (Exception e) {
e.printStackTrace();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.