2011-07-17 5 views
2

xstreamを使用してXMLからスキーマの場所を調べるのに問題があります。 XMLの検証のためXstreamを使用してスキーマの場所のXMLを解析する

<order xmlns="http://www.mycompany.com/xml/myproject" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="test.xsd"> 

、スキーマで、私はjavaxの使用しています:今のところ

Validator validator = schema.newValidator(); 
validator.validate(source); 

を私は「test.xsd」としてスキーマ名をハードコードしましたが、私は願っていますそれは単なる一時的な修正です。

答えて

3

XStreamはデフォルトで名前空間を認識しませんが、有効にすることは可能です。あなたはウェブサイト上の詳細を見つけることができるはずです。しかし、名前空間にアクセスするには、他の属性と同様に扱うことができます。

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 + "'}"; 
    } 
} 
+0

+1 – bbaja42

関連する問題