意外元素(uri:“http://www.w3schools.com”,本地:“武器”)。预期元素是< {} weapon>

时间:2013-10-12 23:59:13

标签: xml jaxb xsd

我有xml文档

<?xml version="1.0"?>

<!--<!DOCTYPE weapon SYSTEM "scheme.dtd">-->

<weapon xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com scheme.xsd">
<!--<weapon>-->
<cold_weapon>
    <knife id="knife1">
        <type>Нож</type>
        <handy>Одноручное</handy>
        <origin>Германия</origin>
        <visual>
            <blade>
                <length>30 см</length>
                <width>40 мм</width>
            </blade>
            <material>сталь</material>
            <handle>
                <tree>бамбук</tree>
            </handle>
            <dol>есть</dol>
        </visual>
        <value>не коллекционный</value>
    </knife>        
</cold_weapon>
</weapon>

我使用xjc生成了java文件。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "coldWeapon" } )
@XmlRootElement(name = "weapon")
public class Weapon {

@XmlElement(name = "cold_weapon", required = true)
protected Weapon.ColdWeapon coldWeapon;

//getters and setters....

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "knife" })
public static class ColdWeapon {

    @XmlElement(required = true)
    protected List<Weapon.ColdWeapon.Knife> knife;

    public List<Weapon.ColdWeapon.Knife> getKnife() {
        if (knife == null) {
            knife = new ArrayList<Weapon.ColdWeapon.Knife>();
        }
        return this.knife;
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = { "type", "handy", "origin", "visual",
            "value" })
    public static class Knife {

        @XmlElement(required = true)
        protected String type;
        @XmlElement(required = true)
        protected String handy;
        @XmlElement(required = true)
        protected String origin;
        @XmlElement(required = true)
        protected Weapon.ColdWeapon.Knife.Visual visual;
        @XmlElement(required = true)
        protected String value;
        @XmlAttribute(name = "id")
        protected String id;

        //getters and setters....

        @XmlAccessorType(XmlAccessType.FIELD)
        @XmlType(name = "", propOrder = { "blade", "material", "handle",
                "dol" })
        public static class Visual {

            @XmlElement(required = true)
            protected Weapon.ColdWeapon.Knife.Visual.Blade blade;
            @XmlElement(required = true)
            protected String material;
            @XmlElement(required = true)
            protected Weapon.ColdWeapon.Knife.Visual.Handle handle;
            @XmlElement(required = true)
            protected String dol;

            //getters and setters

            @XmlAccessorType(XmlAccessType.FIELD)
            @XmlType(name = "", propOrder = { "length", "width" })
            public static class Blade {

                @XmlElement(required = true)
                protected String length;
                @XmlElement(required = true)
                protected String width;

                //getters and setters
            }

            @XmlAccessorType(XmlAccessType.FIELD)
            @XmlType(name = "", propOrder = { "tree" })
            public static class Handle {

                @XmlElement(required = true)
                protected String tree;

                public String getTree() {
                    return tree;
                }

                public void setTree(String value) {
                    this.tree = value;
                }
            }
        }
    }
}
}

我想获得knives的列表。但是,当我尝试使用JAXB解析它时,我遇到了这样的异常unexpected element (uri:"http://www.w3schools.com", local:"weapon"). Expected elements are <{}weapon> 我希望得到knives列表的方法看起来像这样

 public List<Knife> getList(String xsd) throws JAXBException, SAXException {
    JAXBContext jaxbContext = JAXBContext.newInstance(Weapon.class);
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = factory.newSchema(new StreamSource(xsd));
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    jaxbUnmarshaller.setSchema(schema);
    Weapon ts = (Weapon)jaxbUnmarshaller.unmarshal(new File("task.xml"));
    return ts.getColdWeapon().getKnife();
}

在解析之前,我验证文件并成功验证。验证器很简单

public void validateXSD(String xsd) throws SAXException, IOException {
    SchemaFactory factory = SchemaFactory
            .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = factory.newSchema(new StreamSource(xsd));
    Validator validator = schema.newValidator();
    validator.validate(new StreamSource(xml));
}

我的xsd看起来像这样

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"    targetNamespace="http://www.w3schools.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="weapon">
    <xs:annotation>
        <xs:documentation>&lt;!DOCTYPE weapon SYSTEM "scheme.dtd"></xs:documentation>
    </xs:annotation>
    <xs:complexType>
        <xs:sequence>
            <xs:element name="cold_weapon">
                 <xs:annotation>
                    <xs:documentation>&lt;weapon></xs:documentation>
                </xs:annotation>
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="knife" maxOccurs="unbounded" minOccurs="0">
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element type="xs:string" name="type"/>
                                    <xs:element type="xs:string" name="handy"/>
                                    <xs:element type="xs:string" name="origin"/>
                                    <xs:element name="visual">
                                        <xs:complexType>
                                            <xs:sequence>
                                                <xs:element name="blade">
                                                    <xs:complexType>
                                                        <xs:sequence>
                                                            <xs:element type="xs:string" name="length"/>
                                                            <xs:element type="xs:string" name="width"/>
                                                        </xs:sequence>
                                                    </xs:complexType>
                                                </xs:element>
                                                <xs:element type="xs:string" name="material"/>
                                                <xs:element name="handle">
                                                    <xs:complexType mixed="true">
                                                        <xs:sequence>
                                                            <xs:element type="xs:string" name="tree" minOccurs="0"/>
                                                        </xs:sequence>
                                                    </xs:complexType>
                                                </xs:element>
                                                <xs:element type="xs:string" name="dol"/>
                                            </xs:sequence>
                                        </xs:complexType>
                                    </xs:element>
                                    <xs:element type="xs:string" name="value"/>
                                </xs:sequence>
                                <xs:attribute type="xs:string" name="id" use="optional"/>
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>
</xs:schema>

据我所知,似乎命名空间存在问题。 问题在哪里?

1 个答案:

答案 0 :(得分:1)

生成的模型的命名空间信息可能存在于@XmlSchema类的package-info注释中。这个类很可能没有在你的设置中编译。

了解更多信息