xstream将xml解析为具有xml命名空间的java对象
嗨,我需要将xml解析为对象。我正在尝试使用stax进行xstream,因为我的xml有名称空间。由于xsi:schemalocation,我遇到了错误。 有人能帮助我们如何配置schemalocation xsi吗 我得到以下错误: 信息: ContactDelData.javaxstream将xml解析为具有xml命名空间的java对象,java,xml,xstream,Java,Xml,Xstream,嗨,我需要将xml解析为对象。我正在尝试使用stax进行xstream,因为我的xml有名称空间。由于xsi:schemalocation,我遇到了错误。 有人能帮助我们如何配置schemalocation xsi吗 我得到以下错误: 信息: ContactDelData.java public class ContactDelData { private String contactId; private String roid; private Stri
public class ContactDelData {
private String contactId;
private String roid;
private String schemaLocation;
}
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();
}
}
}
ContactDelParser.java
public class ContactDelData {
private String contactId;
private String roid;
private String schemaLocation;
}
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();
}
}
}
import javax.xml.namespace.QName;
导入javax.xml.stream.XMLInputFactory;
导入com.thoughtworks.xstream.xstream;
导入com.thoughtworks.xstream.io.xml.QNameMap;
导入com.thoughtworks.xstream.io.xml.StaxDriver;
公共类ContactDelParser{
公共静态void main(字符串[]args){
String xmlString=“EPP-AB2345
1002703-UK”;
字符串namespaceURI=”http://www.nominet.org.uk/epp/xml/std-notifications-1.2 std-notifications-1.2.xsd”;
字符串别名=“n”;
字符串mappingObjName=“contactDelData”;
类响应=ContactDelData.Class;
QNameMap qmap=新的QNameMap();
QName QName=新的QName(名称空间URI,“别名”,别名);
注册映射(qname,AbuseFeedInfoData.class);
StaxDriver StaxDriver=新StaxDriver(qmap);
XStream xstream2=新XStream(StatxDriver);
别名(mappingObjName,response);
//xstream2.useAttributeFor(响应,“xmlns”);
//别名属性(“xsi:schemaLocation”、“schemaLocation”);
statxdriver.getInputFactory().setProperty(XMLInputFactory.IS\u NAMESPACE\u-AWARE,true);
试一试{
objectpoll=xstream2.fromXML(xmlString);
System.out.println(poll.toString());
}捕获(例外e){
e、 printStackTrace();
}
}
}
它现在已解决。示例xml缺少xmlns:xsi=”“。有一次我补充说它很好用,现在就解决了。示例xml缺少xmlns:xsi=”“。有一次我补充说它很好用。