Skip to content

Instantly share code, notes, and snippets.

@toshimaru
Created June 10, 2012 05:53
Show Gist options
  • Save toshimaru/2904049 to your computer and use it in GitHub Desktop.
Save toshimaru/2904049 to your computer and use it in GitHub Desktop.
dom4j test.
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
public class Dom4jTest {
public static void main(String[] args) {
System.out.println("start");
String xml = "<root><element name='toshi'>text1</element><element2>text2</element2></root>";
try {
Document doc = DocumentHelper.parseText(xml);
// String s = doc.getRootElement().element("element").getText(); //Textとして取得
// String s = doc.getRootElement().element("element").asXML(); //XMLとして取得
// String s = doc.getRootElement().element("element").attributeValue("name");//nameのvalueを取得
doc.getRootElement().element("element").setText("set text!");//Textをセット
System.out.println(doc.asXML());
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment