本文介绍了解组时意外的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have to pull information from a webservice. Here's an example of a response:

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

我得到的错误是这一个:

The error I'm getting is this one:

意外元素(uri:,local:myServices)。预期的元素是< {} myServices >

unexpected element (uri:"", local:"myServices"). Expected elements are <{http://www.nisoars.myservices.1.com}myServices>

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

I can't remove the namespace from the xsd because I need it for other objects. I'm using the javax.bind.xml.Unmarshaller.

任何想法?

推荐答案

应用命名空间



如果输入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);
    }

}



更多信息





  • For More Information

    • http://blog.bdoughan.com/2012/11/applying-namespace-during-jaxb-unmarshal.html
    • 这篇关于解组时意外的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 02:41