在解意外元素(Unexpected element while unmarshalling)

2019-10-20 16:08发布

我不得不从Web服务中提取信息。 这里有一个响应的例子:

<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: “”,当地 “myServices”)。 预期元素是<{ http://www.nisoars.myservices.1.com } myServices>

因为我需要它为其他对象,我不能从XSD删除的命名空间。 我使用的是javax.bind.xml.Unmarshaller。

有任何想法吗?

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

}

执行解组

解组完成利用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);
    }

}

欲获得更多信息

  • http://blog.bdoughan.com/2012/11/applying-namespace-during-jaxb-unmarshal.html


文章来源: Unexpected element while unmarshalling