Reputation: 2169
I'm having trouble finding out the schema location from the XML using the xstream.
<order xmlns="http://www.mycompany.com/xml/myproject"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="test.xsd">
For validation of the XML, with the schema, I'm using the javax :
Validator validator = schema.newValidator();
validator.validate(source);
For now I've hardcoded the schema name as "test.xsd", but I hope that is just a temporary fix.
Upvotes: 2
Views: 2230
Reputation: 128919
XStream isn't namespace-aware by default, though I think you can enable that. You should be able to find details on the website. To just get access to the namespace, though, you can treat it like any other attribute:
public static void main(String[] args) {
String xml = "<x:foo xmlns:x=\"http://foo.com\">" +
"<bar xmlns=\"http://bar.com\"/>" +
"</x:foo>";
XStream xstream = new XStream();
xstream.alias("x:foo", Foo.class);
xstream.useAttributeFor(Foo.class, "xmlns");
xstream.aliasField("xmlns:x", Foo.class, "xmlns");
xstream.alias("bar", Bar.class);
xstream.useAttributeFor(Bar.class, "xmlns");
xstream.aliasField("xmlns", Foo.class, "xmlns");
Object o = xstream.fromXML(xml);
System.out.println("Unmarshalled a " + o.getClass());
System.out.println("Value: " + o);
}
static class Foo {
private String xmlns;
private Bar bar;
public String toString() {
return "Foo{xmlns='" + xmlns + "', bar=" + bar + '}';
}
}
static class Bar {
private String xmlns;
public String toString() {
return "Bar{xmlns='" + xmlns + "'}";
}
}
Upvotes: 3