多年以來,由於沒有處理過XML模式,因此在使用XML Spy中生成的模式手動解組某些XML時遇到了問題。JAXB unmarshalling模式問題:'org.xml.sax.SAXParseException cvc-elt.1'
對於我的生活,我無法解決它,儘管各種其他谷歌類似的問題/答案!
下面是XML(大大減少只是爲了突出這個問題):
<myelement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="./myxsd.xsd">
</myelement>
這裏的myxsd.xsd架構(大大減少只是爲了突出這個問題):
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://myhost.com/Elements" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" targetNamespace="http://myhost.com/Elements" elementFormDefault="qualified" attributeFormDefault="unqualified" vc:minVersion="1.1">
<xs:element name="myelement"/>
</xs:schema>
下面的代碼:
String xml = ""; //input the XML from above.
JAXBContext context =
JAXBContext.newInstance(MyElement.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
document = (MyElement) unmarshaller.unmarshal(new StringReader(xml));
和元素POJO:
@XmlRootElement(name = "myelement", namespace = "http://myhost.com/Elements")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "myelementType", namespace = "http://myhost.com/Elements")
public class MyElement {
}
,導致:
javax.xml.bind.UnmarshalException - with linked exception:[org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 110; cvc-elt.1: Cannot find the declaration of element ‘myelement’.]
實際代碼中的XML是否是字符串?任何理由不只是將文件提供給unmarhsal()? – milez
這只是示例代碼,但傳入的XML仍會導致相同的錯誤。但是,謝謝。 – deanpullen