1

我有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.

@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>

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

4

1 回答 1

1

您生成的模型的命名空间信息可能存在于类的@XmlSchema注释中package-info。此类可能不会在您的设置中编译。

了解更多信息

于 2013-10-13T01:06:55.023 回答