Skip to content

Instantly share code, notes, and snippets.

@creswick
Last active December 14, 2015 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creswick/5040982 to your computer and use it in GitHub Desktop.
Save creswick/5040982 to your computer and use it in GitHub Desktop.
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
@XStreamAlias("sample")
public class Sample {
@XStreamAsAttribute
@XStreamAlias("myns:name")
public final String name;
@XStreamAsAttribute
public final String value;
public final String content;
public Sample(String name, String value, String content) {
super();
this.name = name;
this.value = value;
this.content = content;
}
@Override
public String toString() {
return "Sample [name=" + name + ", value=" + value + ", content="
+ content + "]";
}
}
import java.io.StringReader;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.xml.sax.InputSource;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.Dom4JReader;
public class TestX {
private static String xmlStr1 = "<sample xmlns:myns=\"http://mynamespace\" myns:name=\"some name\" value=\"some val\">" +
"<content>stuff</content>" +
"</sample>";
/**
* @param args
* @throws DocumentException
*/
public static void main(String[] args) throws DocumentException {
XStream xstream = new XStream();
@SuppressWarnings("rawtypes")
Class[] classes = { Sample.class };
xstream.processAnnotations(classes);
SAXReader reader = new SAXReader();
InputSource in = new InputSource( new StringReader( xmlStr1 ) );
Document doc = reader.read(in);
Sample s = (Sample) xstream.unmarshal(new Dom4JReader(doc));
System.out.println(s);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment