簡體   English   中英

解組錯誤預期元素為 <{}>

[英]Unmarshall errorExpected elements are <{}>

我在解組過程中遇到一個奇怪的錯誤。

這是我的解組代碼

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;
    }

}

我沒有使用任何命名空間。 我該如何解決?

看起來您的 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;
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM