0

I am trying to include a text file content into an xml file. I am using the following files. I am getting UnmarshalException on the bar property. Please advice. Many thanks.

thankyou.properties file

Thank you

XML file

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE doc [
<!ELEMENT bar (#PCDATA)>
<!ENTITY otherFile SYSTEM "thankyou.properties">
]>

<doc>
  <foo>
    <bar>&otherFile;</bar>
  </foo>
</doc>

My XSD

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="doc">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="foo">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" name="bar"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Java model

@XmlRootElement(name = "doc")
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {

  @XmlElement(name = "bar")  
  private String bar;

}

The exception I get is

Caused by: javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"bar"). Expected elements are <{
TKM
  • 43
  • 1
  • 6
  • Hi, Can you provide your full exception and the code when you unmarshall your file ? – Dimpre Jean-Sébastien Jun 16 '16 at 12:59
  • In fact when I try with a sample xml files like: ]> &b; it doesn't show any test when I run the xml file from browser. – TKM Jun 16 '16 at 13:08
  • I think I have got my answer as this link says: http://stackoverflow.com/questions/15650009/external-system-entities-are-not-working-for-me-in-chrome-ie-or-netscape-what – TKM Jun 16 '16 at 13:16

1 Answers1

0

The exception you encounter is because your JAXB mapping is incorrect.

Try this :

@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {

  @XmlElement(name = "bar")  
  private String bar;

}

@XmlRootElement(name = "doc")
@XmlAccessorType(XmlAccessType.FIELD)
public class Doc {

  @XmlElement(name = "foo")  
  private Foo foo;

}

Then unmarshall by binding the Doc class to your JAXBContext :

Unmarshaller um = JAXBContext.newInstance(Doc.class).createUnmarshaller();
Doc doc = um.unmarshall(yourFile);

Also, i not sure you will be able to include your file the way you want to do it.

A workaround would be to use XmlAdapter and @XmlJavaTypeAdapter : give the filePath to your bar element, then use the adapter to load the file content.

Dimpre Jean-Sébastien
  • 1,067
  • 1
  • 6
  • 14