由于我已经多年没有使用 XML 模式,因此在使用 XML Spy 中生成的模式手动解码某些 XML 时遇到了问题。
尽管有其他各种类似的谷歌问题/回复,但我一生都无法解决它!
这是 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’.]
最佳答案
似乎存在一个空格“我的元素”。检查myxsd.xsd文件
关于java - JAXB 解码架构问题 : 'org.xml.sax.SAXParseException cvc-elt.1' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38522944/