我在解组时遇到了一个奇怪的错误。
这是我的解码代码
File file = new File("resources/test.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(FuzzyControllerType.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
FuzzyControllerType fct=(FuzzyControllerType) jaxbUnmarshaller.unmarshal(file);
这是我得到的错误:
javax.xml.bind.UnmarshalException: unexpected element
(uri:"", local:"FuzzyController"). Expected elements are <{}fuzzyControllerType>
这是我的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>
我的模糊控制器类型类如下所示:
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;
}
}
我没有使用任何名称空间。我该如何解决这个问题?
答案 0 :(得分:0)
您的XML文档看起来像根元素“FuzzyController”
将注释@XmlRootElement(name="FuzzyController")
添加到班级。
希望它会对你有所帮助。
<强>更新强>
像这样更改你的代码
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;
}
}