Search code examples
xstream

How to pick some information in some tag with XStream


I need to pick some information from a XML and I'm using Xstream. I need some information from books but not every information.

The XML:

<Origin>
 <MsgData>
  <Transaction>
   <Book class="R">
     <Title>info 1</Title>
     <Author>info 2</Author>
     <Pages>info 3</Pages>
       <other infos>some other info that i dont need </other infos>
     <Local class="R">
        ...
        more info that i dont need
        ...
     </Local>
    </Book>
  </Transaction>
 </MsgData>
</Origin>

I tried to use the ignoreUnknownElements() but when I convert the XML to the "Origin" class that I created the attributes from the object are all nulls.

How can I do that?


Solution

  • So I'm using some things to make it work. I created every class until de Book class, so it's like that:

    public class Origin { 
    
      private MsgData msgData;
    
    }
    
    public class MsgData { 
    
      private Transaction transaction;
    
    }
    
    public class Transaction { 
    
      private Book book;
    
    }
    
    public class Book { 
    
     private String info1
     private String info2
     private String info3
    
    }
    

    After that I need to replace the class="R" inside the tag Book. With the Xml I can do a replaceAll()

    Than just do the alias and aliasField from the XStream created and use the ignoreUnknownElements().

    In the end just do the Cast

    OriginfromXml = (Origin)xstreamCreated.fromXml(stringFromReplaceAll);
    

    That's one solution, if someone has some better way to do it I post it.