[英]Unmarshall errorExpected elements are <{}>
I am getting a strange error during unmarshalling.我在解组过程中遇到一个奇怪的错误。
This is my unmarshal code这是我的解组代码
File file = new File("resources/test.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(FuzzyControllerType.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
FuzzyControllerType fct=(FuzzyControllerType) jaxbUnmarshaller.unmarshal(file);
This is the error I get:这是我得到的错误:
javax.xml.bind.UnmarshalException: unexpected element
(uri:"", local:"FuzzyController"). Expected elements are <{}fuzzyControllerType>
This is my xml这是我的 xml
<?xml version="1.0" encoding="UTF-8"?>
<FuzzyController>
<KnowledgeBase>
<FuzzyVariable name="food" domainleft="0.0" domainright="10.0" scale="" type="input">
<FuzzyTerm name="delicious" complement="false">
<LeftLinearShape Param1="5.5" Param2="10.0"/>
</FuzzyTerm>
<FuzzyTerm name="rancid" complement="false">
<TriangularShape Param1="0.0" Param2="2.0" Param3="5.5"/>
</FuzzyTerm>
</FuzzyVariable>
</KnowledgeBase>
</FuzzyController>
My Fuzzy controller type class looks like this:我的模糊控制器类型类如下所示:
package testfuzzy;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "FuzzyControllerType", propOrder = {
"knowledgeBase"
})
@XmlRootElement(name="FuzzyControllerType")
public class FuzzyControllerType {
@XmlElement(name = "KnowledgeBase", required = true)
protected KnowledgeBaseType knowledgeBase;
public KnowledgeBaseType getKnowledgeBase() {
return knowledgeBase;
}
public void setKnowledgeBase(KnowledgeBaseType value) {
this.knowledgeBase = value;
}
}
I haven't used any name spaces.我没有使用任何命名空间。 How do I fix this?
我该如何解决?
It looks like your XML document has the root element "FuzzyController"看起来您的 XML 文档具有根元素“FuzzyController”
Add the annotation @XmlRootElement(name="FuzzyController")
to the class.将注解
@XmlRootElement(name="FuzzyController")
到类中。
Hope it will help you.希望它会帮助你。
UPDATE:更新:
Change your code like this像这样改变你的代码
package testfuzzy;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "FuzzyController", propOrder = {
"knowledgeBase"
})
@XmlRootElement(name="FuzzyController")
public class FuzzyController {
@XmlElement(name = "KnowledgeBase", required = true)
protected KnowledgeBaseType knowledgeBase;
public KnowledgeBaseType getKnowledgeBase() {
return knowledgeBase;
}
public void setKnowledgeBase(KnowledgeBaseType value) {
this.knowledgeBase = value;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.