2013-01-09 3 views
0

두 목록의 요소를 참조해야합니다. XStream을 사용해 보았습니다.XStream에서 두 목록을 서로 참조하는 방법은 무엇입니까?

<bookshop> 
    <authors> 
     <author id="a1"> 
      <name>Stanisław</name> 
     </author> 
    </authors> 
    <books> 
     <book id="b1"> 
      <author>a1</author> 
      <title>Ubik</title> 
      <price currency="PLN">29.0</price> 
     </book> 
    </books> 
</bookshop> 

내 자바 클래스의 약간의 흠집 : 나는 저자 저자에 필드에 널 얻을 클래스로 XML을 넣어려고

public class Bookshop { 
    private ArrayList<Author> authors; 
    private ArrayList<Book> boooks; 
} 

public class Book { 
    @XStreamAsAttribute 
    private String id; 
    private Author author; 
    private String title; 
    private Price price; 
} 

@XStreamConverter(value=ToAttributedValueConverter.class, strings={"value"}) 
public class Price { 
    private double value; 
    @XStreamAsAttribute 
    private String currency; 
} 

public class Author { 
    @XStreamAsAttribute 
    private String id; 
    private String name; 
    private String surname; 
} 

그리고 매번 다음은 XML의 예입니다. 어쩌면 내가 더 많은 주석이 필요하지만 Xstream 문서에서 아무것도 발견하지 못했습니다.

답변

0

XML이 잘못되었습니다. 나는 시험에 사용되는 완전한 소스 코드를 찾을 수 있습니다

xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES); 
xstream.alias("bookshop", Bookshop.class); 
xstream.alias("author", Author.class); 
xstream.alias("book", Book.class); 

:

<bookshop> 
    <authors> 
    <author> 
     <id>a1</id> 
     <name>Yuri</name> 
     <surname>Stanislaw</surname> 
    </author> 
    <author> 
     <id>a2</id> 
     <name>Bill</name> 
     <surname>Gates</surname> 
    </author> 
    </authors> 
    <books> 
    <book> 
     <id>b1</id> 
     <author reference="/bookshop/authors/author[2]"/> 
     <title>Programming basics</title> 
     <price> 
     <currency>USD</currency> 
     <value>100.0</value> 
     </price> 
    </book> 
    </books> 
</bookshop> 

XML 다음과 같은 설정으로 직렬화 : 여기 Bookshop의 인스턴스를 직렬화하려고하면 XStream주는 것입니다 here

참조 문제에 대한 해결책이 충분하지 않다면 XStream과 함께 사용하기 위해 Converter을 쓰는 것이 좋습니다. hort tutorial을 찾을 수 있습니다 here

+0

감사합니다. 물론 틀렸어. 나는 실수로 붙여 넣기/잘라 내기를했다. 미안하다. 그리고 Converter에 대한 나의 의심을 확인해주었습니다. 그래서 나는 좋은쪽으로 가고 있습니다. – user1964668

+0

그러나 왜 변환기가 필요한지 모르겠지만 모든 것이 그대로 작동합니다 ('Bookshop'의 직렬화 및 비 직렬화). 물론 내가 보지 못하는 프로젝트의 측면이있을 수 있습니다. – atomman

+0

네, 잘 작동하지만 xml 파일로 제한됩니다. 이 파일은이 그림에서''라는 참조를 포함 할 수 없으며, 이런 식으로는 ' a32'입니다. – user1964668

관련 문제