public static void main(String[] args) throws Exception { SAXBuilder sb = new SAXBuilder(); // 新建立構造器 Document doc = sb.build(new FileInputStream("company.xml")); // 讀入文件 Element root = doc.getRootElement(); // 獲得根元素element List row = root.getChildren(); //取得節點列表 //按CID查找直接定位到ROW元素返回的是集合 List find = XPath.selectNodes(root, "/ROOT/ROW[@PID=´1´]"); for (int i = 0; i < find.size(); i++) { Element findelement = (Element) find.get(i); System.out.}
//多條件查詢 Element findelement=(Element)XPath.selectSingleNode(root,"/ROOT/ROW[@PID=´3´][@CID=´10´]"); System.out.println(findelement.getAttributeValue("CNAME"));
XMLOutputter outp = new XMLOutputter(Format.getPrettyFormat()); //格式華輸出,產生縮進和換行
//重新格式化 Format format = outp.getFormat(); format.setEncoding("GB2312"); format.setExpandEmptyElements(true); outp.setFormat(format);
//outp.output(doc, new FileOutputStream("jdomcompany.xml")); //輸出XML文檔 outp.output(doc, System.out); System.out.println("JDOM操作XML文檔完畢!"); } }