xstream使用xml命名空间将xml解析为java对象

时间:2017-05-04 03:17:55

标签: java xml xstream

您好我需要将xml解析为object。我正在尝试使用stax xstream,因为我的xml具有命名空间。我因为xsi:schemalocation而收到错误。 任何人都可以帮助如何配置schemalocation xsi?

我收到以下错误: 消息:http://www.w3.org/TR/1999/REC-xml-names-19990114#AttributePrefixUnbound?n:contactDelData&xsi:schemaLocation&xsi

ContactDelData.java

public class ContactDelData {

      private String contactId;
      private String roid;
      private String schemaLocation;
}

ContactDelParser.java

import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.QNameMap;
import com.thoughtworks.xstream.io.xml.StaxDriver;

public class ContactDelParser {

    public static void main(String[] args){

        String xmlString = "<n:contactDelData 
        xmlns:n=\"http://www.nominet.org.uk/epp/xml/std-notifications-1.2\" 
        xsi:schemaLocation=\"http://www.nominet.org.uk/epp/xml/std-notifications-1.2 std-notifications-1.2.xsd\"><n:contactId> EPP-AB2345</n:contactId>
        <n:roid>1002703-UK </n:roid></n:contactDelData>";

        String namespaceURI="http://www.nominet.org.uk/epp/xml/std-notifications-1.2 std-notifications-1.2.xsd";
        String alias="n";
        String mappingObjName="contactDelData";
        Class response=ContactDelData.class;
        QNameMap qmap = new QNameMap();
        QName qname = new QName(namespaceURI, "alias", alias);
        qmap.registerMapping(qname, AbuseFeedInfoData.class);

        StaxDriver staxDriver = new StaxDriver(qmap);
        XStream xstream2 = new XStream(staxDriver);
        xstream2.alias(mappingObjName, response);

        //xstream2.useAttributeFor(response, "xmlns");
        //xstream2.aliasAttribute("xsi:schemaLocation", "schemaLocation");
        staxDriver.getInputFactory().setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);
        try {
            Object poll = xstream2.fromXML(xmlString);
            System.out.println(poll.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

0 个答案:

没有答案