その作品:
import java.io.InputStream;
import org.junit.Test;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import lombok.Data;
public class ColonAlias {
@Test
public void smokeTest() {
InputStream file = ColonAlias.class.getResourceAsStream(ColonAlias.class.getSimpleName()+ ".xml");
XStream xStream = new XStream();
xStream.ignoreUnknownElements();
xStream.processAnnotations(Parent.class);
Parent parent = (Parent) xStream.fromXML(file);
System.out.println(parent);
}
@XStreamAlias("parent")
@Data
public class Parent {
@XStreamAsAttribute
private String name;
@XStreamAlias("p:age")
private String age;
}
}
XML:
<?xml version="1.0" encoding="UTF-8"?>
<parent name="Adam">
<p:age>33</p:age>
</parent>
出力:
ColonAlias.Parent(name=Adam, age=33)