2011-07-17 2 views
2

xstream을 사용하여 XML에서 스키마 위치를 찾는 데 문제가 있습니다. "test.xsd"로 지금은 하드 코딩 한 스키마 이름은Xstream을 사용하여 스키마 위치의 XML 구문 분석

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

, 그러나 나는 희망 : 스키마와 XML의 검증을 위해

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

, 나는 javax의를 사용하고 있습니다 그것은 단지 일시적인 수정입니다.

답변

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