How to use DTD ENTITY external reference in log4j.xml - java

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;
]>

Related

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>
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)

Is there a way to manage repetitive image path in fxml?

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/">

Writing an XML in Matlab: How to add reference to DTD?

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>

Parsing xml data

DOes anyone know how to resolve this exception??
java.io.FileNotFoundException: F:\eclipse\WS\l\dblp.dtd (The system cannot find the file specified)
Even though I have given the correct path still Im getting this exception.
heres my xml code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE dblp SYSTEM "dblp.dtd">
<dblp>
<article mdate="2011-01-11" key="journals/acta/Saxena96">
<author>Sanjeev Saxena</author>
<title>Parallel Integer Sorting and Simulation Amongst CRCW Models. </title>
<pages>607-619</pages>
<year>1996</year>
<volume>33</volume>
<journal>Acta Inf.</journal>
<number>7</number>
<url>db/journals/acta/acta33.html#Saxena96</url>
<ee>http://dx.doi.org/10.1007/BF03036466</ee>
</article>
<article mdate="2011-01-11" key="journals/acta/Simon83">
<author>Hans-Ulrich Simon</author>
<title>Pattern Matching in Trees and Nets.</title>
<pages>227-248</pages>
<year>1983</year>
<volume>20</volume>
<journal>Acta Inf.</journal>
<url>db/journals/acta/acta20.html#Simon83</url>
<ee>http://dx.doi.org/10.1007/BF01257084</ee>
</article>
</dblp>
You are referencing "dblp.dtd" in your DOCTYPE - do you have that DTD file in the directory mentioned in the exception?
If not and you don't have the DTD, try removing the DOCTYPE line from the xml file, or overriding the entity resolution to tell it not to try loading it, as in this answer.

How to correct for no grammar constraints error

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>

Categories

Resources