Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JAXB解组模式问题:';org.xml.sax.SAXParseException cvc elt.1';_Java_Xml_Xsd_Jaxb - Fatal编程技术网

Java JAXB解组模式问题:';org.xml.sax.SAXParseException cvc elt.1';

Java JAXB解组模式问题:';org.xml.sax.SAXParseException cvc elt.1';,java,xml,xsd,jaxb,Java,Xml,Xsd,Jaxb,由于多年没有使用XML模式,我在使用XMLSpy中生成的模式手动解组一些XML时遇到了问题 就我的一生而言,我无法解决它,尽管谷歌提出了各种类似的问题/回答 以下是XML(为了突出问题而大幅缩减): 以及元素pojo: @XmlRootElement(name = "myelement", namespace = "http://myhost.com/Elements") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "myelem

由于多年没有使用XML模式,我在使用XMLSpy中生成的模式手动解组一些XML时遇到了问题

就我的一生而言,我无法解决它,尽管谷歌提出了各种类似的问题/回答

以下是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文件

似乎存在一个空格“我的元素”。检查myxsd.xsd文件

公共静态MyElement unmarshal(String str)抛出jaxbeexception{
public static MyElement unmarshal(String str) throws JAXBException {
 JAXBContext jaxbContext = JAXBContext.newInstance(MyElement.class);
 Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

 JAXBElement<MyElement> root = jaxbUnmarshaller.unmarshal(new StreamSource(new StringReader(str))), MyElement.class);
 MyElement el = root.getValue();

 return el;
}
JAXBContext JAXBContext=JAXBContext.newInstance(MyElement.class); 解组器jaxbUnmarshaller=jaxbContext.createUnmarshaller(); JAXBElement root=jaxbUnmarshaller.unmarshal(newstreamsource(newstringreader(str))),MyElement.class; MyElement el=root.getValue(); 返回el; }
可能与上面类似。

publicstaticmyelement unmarshal(stringstr)抛出jaxbeexception{
JAXBContext JAXBContext=JAXBContext.newInstance(MyElement.class);
解组器jaxbUnmarshaller=jaxbContext.createUnmarshaller();
JAXBElement root=jaxbUnmarshaller.unmarshal(newstreamsource(newstringreader(str))),MyElement.class;
MyElement el=root.getValue();
返回el;
}

可能与上面类似。

您的XML字符串是否在实际代码中?是否有理由不将文件馈送到unmarhsal()?这只是示例代码,但是传入XML仍然会导致相同的错误。但是,谢谢。您的XML字符串是否在实际代码中?是否有理由不将文件馈送到unmarhsal()?这只是示例代码,但是传入XML仍然会导致相同的错误。但是谢谢。谢谢。但是那里似乎没有空格?那么消息就找不到元素“my element”的声明。很奇怪,因为它看起来好像有一个空间。也许在Sax失败的地方进行调试可能会带来一些帮助和歉意;我没有意识到你的意思在错误-这只是我复制和粘贴,然后编辑错误,为了使类匿名等,空间不应该在那里,我已经更新了错误。谢谢没问题。XSD是否可以访问?也许JAx无法获取它,谢谢,但是那里似乎没有空格?那么消息就找不到元素“my element”的声明。很奇怪,因为它看起来好像有一个空间。也许在Sax失败的地方进行调试可能会带来一些帮助和歉意;我没有意识到你的意思在错误-这只是我复制和粘贴,然后编辑错误,为了使类匿名等,空间不应该在那里,我已经更新了错误。谢谢没问题。XSD是否可以访问?也许JAx拿不到它
@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’.]
public static MyElement unmarshal(String str) throws JAXBException {
 JAXBContext jaxbContext = JAXBContext.newInstance(MyElement.class);
 Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

 JAXBElement<MyElement> root = jaxbUnmarshaller.unmarshal(new StreamSource(new StringReader(str))), MyElement.class);
 MyElement el = root.getValue();

 return el;
}