-- 作者:xiaoshuyu
-- 发布时间:4/30/2005 11:52:00 AM
--
import java.io.*; import java.util.*; import javax.xml.parsers.*; import org.apache.crimson.tree.XmlDocument;// import org.xml.sax.*; import org.w3c.dom.*; public class Test{ public Test(){ } public InputSource parseXml(String inXml){ if (inXml == null) { throw new IllegalArgumentException(); } try { return new InputSource(new StringReader(inXml)); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } } public static void main(String[] args){ Test tempTest = new Test(); StringBuffer xmlString = new StringBuffer(); xmlString.append("<?xml version=\"1.0\"?>\n"); xmlString.append("<userAuth>\n"); xmlString.append(" <token>").append("704e68fb6ccfc3e3f1b6d7ac41291c62").append("</token>\n"); xmlString.append(" <username>").append("2509409").append("</username>\n"); xmlString.append(" <serviceid>").append("001").append("</serviceid>\n"); xmlString.append(" <contentid>").append("4").append("</contentid>\n"); xmlString.append(" <contentname>").append("test").append("</contentname>\n"); xmlString.append(" <price>").append("53").append("</price>\n"); xmlString.append(" <police>").append("1").append("</police>\n"); xmlString.append(" <ip>").append("192.168.0.2").append("</ip>\n"); xmlString.append(" <spid>").append("sp001").append("</spid>\n"); xmlString.append("</userAuth>"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = null; try { db = dbf.newDocumentBuilder(); } catch (ParserConfigurationException pce) { System.err.println(pce); //³öÒ쳣ʱÊä³öÒì³£ÐÅÏ¢£¬È»ºóÍ˳ö£¬ÏÂͬ System.exit(1); } Document doc = null; try { doc = db.parse(tempTest.parseXml(xmlString.toString())); } catch (Exception dom) { System.err.println(dom.getMessage()); System.exit(1); } Element root = doc.getDocumentElement(); System.out.println(root.toString()); } } }
|