简体   繁体   English

解组错误预期元素为 <{}>

[英]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.

相关问题 JAXB - 不要解组嵌套元素 - 可能吗? - JAXB - DO NOT unmarshall nested elements - possible? 如何使用JAXB解组仅子元素? - How to unmarshall only child elements using JAXB? JAXB解组具有不同属性的相同元素(lang) - JAXB Unmarshall same elements with different attributes (lang) 使用JAXB解组具有不同/动态名称的元素 - Using JAXB to unmarshall elements with varying/dynamic names JaxB Unmarshall错误:java.lang.RuntimeException:意外元素(uri:“”,local:“code”)。 预期的元素是&lt;{} section&gt; - JaxB Unmarshall error : java.lang.RuntimeException: unexpected element (uri:“”, local:“code”). Expected elements are <{}section> xml unmarshall错误jax -b预期元素中存在意外元素 - xml unmarshall error jax-b a unexpected element that exists in the expected elements JAXB 解组错误:UnmarshalException-意外元素-预期元素是(无)-@XmlRootElement 没有丢失 - JAXB Unmarshall Error: UnmarshalException- Unexpected Element - Expected elements are (none) - @XmlRootElement NOT missing Java / JAXB:将具有相同名称但不同属性值的XML元素解组为不同的类成员 - Java/JAXB: Unmarshall XML elements with same name but different attribute values to different class members Unmarshall Jaxb Opennms API - Unmarshall jaxb opennms api Jaxb Unmarshall带有未知的@XmlRootElement - Jaxb Unmarshall with an unknown @XmlRootElement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM