Details
-
Type: New Feature
-
Status: Closed
-
Resolution: Fixed
-
Affects Version/s: None
-
Fix Version/s: 1.4
-
Component/s: None
-
Labels:None
-
JDK version and platform:Apple JDK 1.5.0_06, Mac OS 10.4.6 (PPC)
Description
I have a number of XStream serialised files which have been hand edited to refer to a DTD containing entity declarations to encapsulate data (file paths in my case) which is shared between the files.
If I try to use the XStream.fromXML methods to load these files it fails, as the parser does not have the system ID of the XML file and so is unable to locate the DTD. It would be nice if XStream provided a fromXML(URL) method that passes the system ID to the parser, as the workaround involves explicitly creating e.g. an XMLStreamReader and StaxReader to pass to XStream.unmarshal, which ties the code to one particular driver.
Hello,
I have the same problem. I thought the solution would be adding a XStream.fromXML(File), but maybe the XStream.fromXML(URL) that Ian suggests is more ellegant. I would vote for both, anyway.
I use XML files like this:
sample.xml
-----------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE person SYSTEM "sample.dtd" [
<!ENTITY p SYSTEM "person.xml">
]>
<person>
&p;
</person>
-----------------------------------------------------------------------
This file references an external DTD (sample.dtd) and an included XML (person.xml).
When you parse from InputStream or Reader, using XStream or directly a DocumentBuilder, referenced files (sample.dtd and person.xml) are not searched in the same folder as sample.xml, as it could be expected. I think they are searched in the classpath.
On the other hand, if you parse sample.xml using DocumentBuilder.parse(File) method, it looks for referenced files in the same folder.
If there was a XStream.fromXml(File) method, I could use it directly:
Person person = (Person) xstream.fromXml( new File("sample.xml") );
But, since it doesn't exist, I'm forced to do this:
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse( new File("sample.xml") );
Person person = (Person) xstream.unmarshal( new DomReader(doc) );
Regards,
ranMa