Sample XML
Java Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | import org.jdom.*; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; class test { public static final String SERVER_INFO_ELE = "serverinfo"; public static final String SERVER_ELE = "server"; public static final String ID_ATTR = "id"; public static final String IP_ELE = "ip"; public static final String PORT_ELE = "port"; public static final String DESC_ELE = "desc"; public static void main(String[] args) throws Exception { Element root = new Element(SERVER_INFO_ELE); { Element srv = new Element(SERVER_ELE); srv.setAttribute( new Attribute(ID_ATTR, "idx1") ); srv.addContent( new Element(IP_ELE).setText("127.0.0.1") ); srv.addContent( new Element(PORT_ELE).setText("8080") ); Element desc = new Element(DESC_ELE); CDATA cdata = new CDATA("This Server is idx <1>."); desc.addContent(cdata); srv.addContent(desc); root.addContent(srv); } { Element srv = new Element(SERVER_ELE); srv.setAttribute( new Attribute(ID_ATTR, "idx2") ); srv.addContent( new Element(IP_ELE).setText("127.0.0.2") ); srv.addContent( new Element(PORT_ELE).setText("8080") ); Element desc = new Element(DESC_ELE); CDATA cdata = new CDATA("This Server is idx <2>."); desc.addContent(cdata); srv.addContent(desc); root.addContent(srv); } XMLOutputter xo = new XMLOutputter(); xo.setFormat(Format.getPrettyFormat()); String resultXML = xo.outputString( new Document(root) ); System.out.println(resultXML); } // end - main() } // end - test |
'Programming' 카테고리의 다른 글
Frequently used bash commands (0) | 2014.03.31 |
---|---|
Comparing bash script / windows cmd script (0) | 2013.11.08 |
Frequently used(referred) Python code (0) | 2013.05.24 |
Java sample code of random string generator (0) | 2013.05.15 |
JSP Sample code of Http Post (0) | 2013.05.15 |
댓글