Last active
December 14, 2015 06:09
-
-
Save creswick/5040982 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + "]"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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