javaxmldom4j

How to define XML entities in Java using Dom4J?


I'm trying to add entity declarations to my XML document in Java using Dom4J 2.1.1, but can't figure out how to do so, or if it's even possible. Can anyone help please?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE TEAMS_ASSET_FILE PUBLIC "-//TEAMS//DTD asset and link file//EN" "D:\Apps\data\Tasset.dtd" [
<!ENTITY  asset0000001 SYSTEM "Z:\somepath\1234\myfile.pdf">
<!ENTITY  asset0000002 SYSTEM "Z:\anotherpath\5678\another.pdf">
]>
<content>
...
</content>

Solution

  • see DocumentType-Interface [https://dom4j.github.io/javadoc/2.1.1/org/dom4j/DocumentType.html]. Get it by using DocumentType docType = doc.getDocType(); and then add entities as InternalEntityDecl/ExternalEntityDecl with setExternalDeclarations(java.util.List<Decl> declarations) or setInternalDeclarations(java.util.List<Decl> declarations)