Xstream library generating xml with empty node

Asked

Viewed 120 times

0

I’m using the library Xstream 1.4.8 with the following XML:

<root>
    <att1>1</att1>
    <att2>2</att2>
  <nodeB xmlns:d8p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    <d8p1:string>00042052</d8p1:string>
    <d8p1:string>00042053</d8p1:string>
    <d8p1:string>00042054</d8p1:string>
    <d8p1:string>00042055</d8p1:string>
  </nodeB>
<root/>

Converting the previous xml into an Entity and generating the xml again <nodeB> has only spaces, and in the java entity the field has an item in the all-blank list. But it should have 4 items in the list. See the resulting xml below:

<root>
 <att1>1</att1>
 <att2>2</att2>
 <nodeB>     


 </nodeB>
<root/>

The problem is when I use <d8p1:string> then the parser generates the empty nodes and generates no error.

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

@XStreamAlias("root")
public class Root {

    @XStreamAlias("att1")
    @XStreamAsAttribute
    private int att1;

    @XStreamAlias("att2")
    @XStreamAsAttribute
    private int att2;

    @XStreamAlias("nodeB") // Se eu colocar este campo gera erro ao encontrar o 'path : /root/nodeB/d8p1:string'
    private NodeB nodeB;

//  @XStreamImplicit(itemFieldName = "nodeB")
//  private List<String> d8p1 = new ArrayList<String>();

//  public List<String> getD8p1() {
//      return d8p1;
//  }
//
//  public void setD8p1(List<String> d8p1) {
//      this.d8p1 = d8p1;
//  }

    public int getAtt1() {
        return att1;
    }

    public void setAtt1(int att1) {
        this.att1 = att1;
    }

    public int getAtt2() {
        return att2;
    }

    public void setAtt2(int att2) {
        this.att2 = att2;
    }

    public NodeB getNodeB() {
        return nodeB;
    }

    public void setNodeB(NodeB nodeB) {
        this.nodeB = nodeB;
    }
}


import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamImplicit;

@XStreamAlias("nodeB")
public class NodeB {

//  @XStreamAlias("str")
//  @XStreamAsAttribute
//  private String str; 

    @XStreamImplicit(itemFieldName = "d8p1")
//  @XStreamAlias("d8p1")
    @XStreamAsAttribute
    private List<String> d8p1 = new ArrayList<String>();

    public List<String> getD8p1() {
        return d8p1;
    }

    public void setD8p1(List<String> d8p1) {
        this.d8p1 = d8p1;
    }

//  public String getStr() {
//      return str;
//  }
//
//  public void setStr(String str) {
//      this.str = str;
//  }

}

Testing

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class Teste {

    public static void main(String[] args) {
        try {
            String xml = 
                    "<root>"
                    + "<att1>1</att1>"
                    + "<att2>2</att2>"
                    + "<nodeB xmlns:d8p1='http://schemas.microsoft.com/2003/10/Serialization/Arrays'>"
                    +   "<d8p1:string>00042052</d8p1:string>"
                    +   "<d8p1:string>00042053</d8p1:string>"
                    +   "<d8p1:string>00042054</d8p1:string>"
                    +   "<d8p1:string>00042055</d8p1:string>"
                    + "</nodeB>"
                    + "</root>";
            XStream xStream = new XStream(new DomDriver());
            xStream.processAnnotations(Root.class);

            Root root = (Root) xStream.fromXML(xml);
            assert root != null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • 1

    This site is for questions in Portuguese, translate your question.

  • 1

    Could you please post the code you are using to convert the xmls?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.