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>
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)
Related
I'm wondering what is the better way to mangage path in my fxml file ?
For example, I have many fxml files with :
Is there a way to store /ressources/images/ somewhere and do something like this :
<Image url="#MY_DEFINE_PATH/success_128.png" />
where MY_DEFINE_PATH come from a global file somewhere in my project ?
You could embed your own DTD entity definition:
<?xml ... ?>
<!DOCTYPE GridPane [
<!ENTITY imgPath "/ressources/images/">
]>
<GridPane>
...
<Image url="&imgPath;success_128.png" />
You could also link to an external DTD, but then one should use an XML catalog (DTDs in local cache mapping from their URLs) to speed up processing of the XML.
Using an external DTD file
<!DOCTYPE GridPane [
<!ENTITY imgPath "/mypaths.dtd">
]>
mypaths.dtd:
<!ENTITY imgPath "/ressources/images/">
I'm trying to write an XML file using Matlab and I need to specify a DOCTYPE DTD at the header, but I haven't found any method for this in the Matlab documentation or questions related. Every question involving a DTD reference is about how to read an XML into Matlab.
What I am able to do now is an XML file of the type
<?xml version="1.0"?>
<root>
<child>
Hello world!
</child>
</root>
with the code
docNode = com.mathworks.xml.XMLUtils.createDocument('root');
root = docNode.getDocumentElement;
child = docNode.createElement('child');
child.appendChild(docNode.createTextNode('Hello World!'));
root.appendChild(child);
xmlwrite(docNode)
However, I need the file to include a DTD reference:
<?xml version="1.0"?>
<!DOCTYPE root SYSTEM "root.dtd" []>
<root>
<child>
Hello world!
</child>
</root>
Is there any function in com.mathworks.xml.XMLUtils for this? Or will I have to open the generated XML and manually insert the DTD reference?
You can stay with using the org.w3c.dom package: you can use the createDocumentType method of DOMImplementation.
domImpl = docNode.getImplementation();
doctype = domImpl.createDocumentType('root', 'SYSTEM', 'root.dtd');
With this update the full sample code is:
docNode = com.mathworks.xml.XMLUtils.createDocument('root');
domImpl = docNode.getImplementation();
doctype = domImpl.createDocumentType('root', 'SYSTEM', 'root.dtd');
docNode.appendChild(doctype);
root = docNode.getDocumentElement;
child = docNode.createElement('child');
child.appendChild(docNode.createTextNode('Hello World!'));
root.appendChild(child);
xmlwrite(docNode)
Output
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE root PUBLIC "SYSTEM" "root.dtd">
<root>
<child>Hello World!</child>
</root>
I have a simple XML document that is flagged (correctly) in Eclipse as having no grammar. I use the file to preload a database on initialisation. Is there a "generic" DTD or Schema that I could apply to this document (and similar - I have over 15 of them) to eliminate this warning and be more correct in my XML structure?
<AbnormalFlags>
<AbnormalFlag>
<code>H</code>
<description>High</description>
</AbnormalFlag>
<AbnormalFlag>
<code>L</code>
<description>Low</description>
</AbnormalFlag>
<AbnormalFlag>
<code>A</code>
<description>Abnormal</description>
</AbnormalFlag>
</AbnormalFlags>
Just add this on top of your xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
I have below xml. Namespace is missing in the below xml.
<?xml version="1.0" encoding="UTF-8"?>
<policy>
<num-drivers>123</num-drivers>
<risk-policy-ind>false</risk-policy-ind>
<premium-amt>23.00</premium-amt>
</policy>
Looking for java code to take the above xml as input and add namespace (xmlns) element to it? The expected output xml is as below:
<?xml version="1.0" encoding="UTF-8"?>
<policy xmlns="http://aaa.bbb.com">
<num-drivers>123</num-drivers>
<risk-policy-ind>false</risk-policy-ind>
<premium-amt>23.00</premium-amt>
</policy>
First of all, in the above xml, risk-policy-ind tag is not properly closed. In XML all tags are custom tags, they should close. Moreover,in xml, tags are performed without namespace.
If u just want to add xmlns attribute to policy tag, create policy element using w3c.dom.Element and set attribute using setAttribute function
I tried to use entities from external dtd file.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"
[<!ENTITY logHome SYSTEM "log4j-entity.dtd">]
>
log4j-entity.dtd
<?xml version="1.0" encoding="UTF-8"?>
<!ENTITY logHome "/root/crm_test/">
I tried to use the entity values in attribute values like this.
<param name="File" value="&logHome;log/info.log"/>
I get this errror:
The external entity reference "&logHome;" is not permitted in an attribute value.
How can I do this?
Note:
This thing works..
<!ENTITY logHome "/root/crm_test/">
You need to make the entity inside the internal subset a parameter entity and then reference it.
Change:
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"
[<!ENTITY logHome SYSTEM "log4j-entity.dtd">]
>
to:
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" [
<!ENTITY % logHome SYSTEM "log4j-entity.dtd">
%logHome;
]>
The XML specs specifically forbid the use of external entities in attribute values. See here: http://www.w3.org/TR/2004/REC-xml-20040204/#forbidden
The following are forbidden, and constitute fatal errors: [...] a reference to an external entity in an attribute value.
So the answer is: XML won't let you do what you're trying to do. You might, however, get a similar effect if you ran your XML through an XSLT processor and applied transformations as needed.
There are a couple things wrong here.
You are using the entity name logHome for two different things (an external entity containing declarations, which should as Daniel Haley points out be a parameter entity) and an internal entity whose replacement text names a directory.
As a consequence, your reference to &logHome; in the attribute value is understood to be a reference to the resource whose URI is "log4j-entity.dtd".
The simplest way to achieve what you want would be to declare the logHome entity in the internal subset:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" [
<!ENTITY logHome "/root/crm_test/">
]>
If you really want the declaration of logHome to be external, it might be less confusing to use a different name for the parameter entity:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" [
<!ENTITY % logHomeDeclaration SYSTEM "log4j-entity.dtd">
%logHomeDeclaration;
]>