解组时意外的元素

时间:2014-08-11 18:56:06

标签: java xml jaxb xsd unmarshalling

我必须从网络服务中提取信息。以下是回复的示例:

<myServices>
<myService name="A" serviceId="a" type="rest" version="1.0">
<entryPoints/>
</myService>
<myService name="B" serviceId="b" type="rest" version="1.0">
<entryPoints/>
</myService>
<myService name="C" serviceId="C" type="rest" version="1">
<entryPoints>
<entryPoint realm="external" wadl="http://myURL.com/1/app.wadl>http://myURL.com/1</entryPoint>
</entryPoints>
</myService>
<myService name="D" serviceId="d" type="rest" version="1.0">
<entryPoints/>
</myService>
</myServices>

这是我的xsd:

<?xml version="1.0" encoding="windows-1252" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://www.nisoars.myservices.1.com"
            targetNamespace="http://www.nisoars.myservices.1.com"
            elementFormDefault="qualified">

    <xsd:element name="myServices">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="myService" type="ServiceType" minOccurs="1" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>  

    <xsd:complexType name="ServiceType">
        <xsd:sequence>
            <xsd:element name="entryPoints">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="entryPoint" type="EntryPointType" minOccurs="1" maxOccurs="unbounded"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence>
        <xsd:attribute name="name" type="xsd:string" use="required"/>
        <xsd:attribute name="serviceId" type="xsd:string" use="required"/>
        <xsd:attribute name="type" type="ServiceTypeEnum" use="required"/>
        <xsd:attribute name="version" type="xsd:string" use="required"/>
    </xsd:complexType>

    <!-- <entryPoint wadl="myWadl" realm="external">myURL</entryPoint> -->
    <xsd:complexType name="EntryPointType">
        <xsd:simpleContent>
            <xsd:extension base="xsd:anyURI">
                <xsd:attribute name="realm" type="RealmEnum" use="required"/>
                <xsd:attribute name="wadl" use="optional" type="xsd:anyURI"/> <!-- Required for REST endpoints, but not for SOAP -->
            </xsd:extension>
        </xsd:simpleContent>
    </xsd:complexType>


    <xsd:simpleType name="RealmEnum">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="internal"/>
            <xsd:enumeration value="external"/>
        </xsd:restriction>
    </xsd:simpleType>

    <xsd:simpleType name="ServiceTypeEnum">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="rest"/>
            <xsd:enumeration value="soap"/>
        </xsd:restriction>
    </xsd:simpleType>
</xsd:schema>

我得到的错误就是这个:

意外元素(uri:&#34;&#34;,local:&#34; myServices&#34;)。预期的元素是<{{{}}} myServices&gt;

我无法从xsd中删除命名空间,因为我需要它用于其他对象。我使用的是javax.bind.xml.Unmarshaller。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

应用命名空间

如果输入XML没有命名空间,您可以利用SAX XMLFilter来应用命名空间。

import org.xml.sax.*;
import org.xml.sax.helpers.XMLFilterImpl;

public class NamespaceFilter extends XMLFilterImpl {

    private static final String NAMESPACE = "http://www.nisoars.myservices.1.com/";

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        super.endElement(NAMESPACE, localName, qName);
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes atts) throws SAXException {
        super.startElement(NAMESPACE, localName, qName, atts);
    }

}

Unmarshal

利用JAXB UnmarshallerHandler作为ContentHandler

进行解组
import javax.xml.bind.*;
import javax.xml.parsers.*;
import org.xml.sax.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        // Create the JAXBContext
        JAXBContext jc = JAXBContext.newInstance(Customer.class);

        // Create the XMLFilter
        XMLFilter filter = new NamespaceFilter();

        // Set the parent XMLReader on the XMLFilter
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        filter.setParent(xr);

        // Set UnmarshallerHandler as ContentHandler on XMLFilter
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        UnmarshallerHandler unmarshallerHandler = unmarshaller
                .getUnmarshallerHandler();
        filter.setContentHandler(unmarshallerHandler);

        // Parse the XML
        InputSource xml = new InputSource("input.xml");
        filter.parse(xml);
        Customer customer = (Customer) unmarshallerHandler.getResult();

        // Marshal the Customer object back to XML
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(customer, System.out);
    }

}

更多信息