问题
I need to unmarshal !DOCTYPE section using JAXB so I can get the values of file1 and file2.
The xml file looks like this :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE doc [
<!ENTITY file1 SYSTEM "conf/files/file1.xml">
<!ENTITY file2 SYSTEM "conf/files/file2.xml">
]>
<appels>
<appel>
&file1;
</appel>
<appel>
&file1;
</appel>
</appels>
the java look like this :
@XmlRootElement(name = "appels")
public class Appels implements Serializable {
private List<Appel> appel;
}
I need to get file1 = conf/files/file1.xml and file2 = conf/files/file2.xml
is this possible? Thank you for the help
回答1:
UPDATE
The following will resolve &file1;
to the contents of the file conf/files/file1.xml
.
<!ENTITY file1 SYSTEM "conf/files/file1.xml">
The following will resolve &file3;
to the string conf/files/file3.xml
.
<!ENTITY file3 "conf/files/file3.xml">
Input Files
input.xml
This is a slightly modified version of the XML document from your question. I changed one of the &file1;
to &file2;
and added &file3;
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE doc [
<!ENTITY file1 SYSTEM "conf/files/file1.xml">
<!ENTITY file2 SYSTEM "conf/files/file2.xml">
<!ENTITY file3 "conf/files/file3.xml">
]>
<appels>
<appel>
&file1;
</appel>
<appel>
&file2;
</appel>
<appel>
&file3;
</appel>
</appels>
conf/files/file1.xml
<foo>Hello World</foo>
conf/files/file2.xml
Instead of making this an XML file, I'm just going to add text.
Foo Bar
Equivalent XML File
As far as your parser is concerned your XML document looks like the following. Note that &file1;
and &file2;
resolved to the contents of their files and &file3;
did not.
<?xml version="1.0" encoding="UTF-8"?>
<appels>
<appel>
<foo>Hello World</foo>
</appel>
<appel>
Foo Bar
</appel>
<appel>
conf/files/file3.xml
</appel>
</appels>
Demo Code
Demo
The demo code below will convert the XML to objects, and then write it back to XML. The entities are resolved when the XML is unmarshalled into objects.
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Appels.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum20637070/input.xml");
Appels appels = (Appels) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(appels, System.out);
}
}
Output
Note how the first appel
element marshalled. Since &file;
resolved to an XML fragment, JAXB wasn't able to match it against the text property we had mapped.
<appels>
<appel>
</appel>
<appel>
Foo Bar
</appel>
<appel>
conf/files/file3.xml
</appel>
</appels>
Java Model
Below is the Java model I used for this example:
Appels
import java.io.Serializable;
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement(name = "appels")
@XmlAccessorType(XmlAccessType.FIELD)
public class Appels implements Serializable {
private List<Appel> appel;
}
Appel
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Appel {
@XmlValue
private String value;
}
回答2:
It seems to me that this is what you're looking for: http://lauraliparulo.altervista.org/jaxb-part-6-entities-references-expansion-before-unmarshalling/
来源:https://stackoverflow.com/questions/20637070/how-to-unmarshal-doctype-section-in-jaxb