StringBuffer sb=new StringBuffer("<?xml version=\"1.0\" encoding=\"gb2312\"?>");
sb.append("<users>");
sb.append("<user id=\"1\">");
sb.append("<name>龙准</name>");
sb.append("<age>25</age>");
sb.append("</user>");
sb.append("<user id=\"2\">");
sb.append("<name>廖丽</name>");
sb.append("<age>21</age>");
sb.append("</user>");
sb.append("</users>");
Document doc=null;
SAXBuilder builder=new SAXBuilder();
//为文档增加dtd验证
//Builder.setFeature(“http://xml.org/sax/features/validation”;,ture);
//为文档增加schema验证
//Builder.setFeature(“http://apache.org/xml/features/validation/schema”;,true);
//导入schema文件
Builder.setPropertity(“http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation”;,schema)
try {
doc=builder.build(new StringReader(sb.toString()));
//得到根节点
Element element=doc.getRootElement();
//得到所有节点
List list=element.getChildren();
System.out.println("某班共有"+list.size()+"个学生");
for(Iterator it=list.iterator();it.hasNext();){
Element user=(Element)it.next();
String id=user.getAttributeValue("id");
String name=user.getChildText("name");
String age=user.getChildText("age");
System.out.println("学生"+id+" ");
System.out.println("姓名:"+name+" 年龄:"+age);
}
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
输出结果:
某班共有2个学生
学生1
姓名:龙准 年龄:25
学生2
姓名:廖丽 年龄:21
新增节点:
//新增节点
Element newElement=new Element("user");
newElement.setAttribute("id","3");
newElement.addContent(new Element("name").setText("嘻哈"));
newElement.addContent(new Element("age").setText("23"));
element.addContent(newElement);
//删除节点
list.remove(2); //删除第三个子元素
element.removeChildren("user"); //删除名为user的子元素
//生成xml文档
Element root=new Element("student-info");
Element stu=new Element("student");
stu.addContent(new Element("number").setText("001"));
stu.addContent(new Element("name").setText("中国"));
stu.addContent(new Element("age").setText("60"));
root.addContent(stu);
Document doc=new Document(root);
Format format=Format.getCompactFormat();
format.setEncoding("gb2312");
format.setIndent(" ");//设置xml文件的缩进为4个空格
XMLOutputter op=new XMLOutputter(format);
try {
op.output(doc, new FileOutputStream("d://student22.xml"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//生成xml文档2
Document document=new Document();
//为xml文档添加处理指令
ProcessingInstruction pi=new ProcessingInstruction("xml-stylesheet","type='text/xsl' href=test.xsl" );
document.addContent(pi);
//添加名称空间
Namespace namespace=Namespace.getNamespace("http://www.apache.org");
Namespace namespace2=Namespace.getNamespace("other", "http://www.w3c.org");
//创建带有名称空间的根元素
Element root1=new Element("student-info",namespace);
root1.addNamespaceDeclaration(namespace2);
Element stu=new Element("student");
stu.setAttribute("id","001");
stu.addContent(new Text("第一个学生"));
stu.addContent(new Element("name").setText("ss"));
stu.addContent(new Element("age").setText("24"));
Element stu2=new Element("student");
stu2.setAttribute("id","002");
stu2.addContent(new Text("第二个学生"));
stu2.addContent(new Element("name").setText("dd"));
stu2.addContent(new Element("age").setText("22"));
root1.addContent(stu);
root1.addContent(stu2);
document.addContent(root1);
Format fm=Format.getCompactFormat();
fm.setEncoding("gb2312");
fm.setIndent(" ");
XMLOutputter xmlout=new XMLOutputter(fm);
try {
xmlout.output(document, new FileOutputStream("d://3.xml"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
JDOM+Xpath读取,修改xml
SAXBuilder builder=new SAXBuilder();
try {
Document doc=builder.build(new File("d://user2.xml"));
XPath userPath=XPath.newInstance("//user");
List userList=userPath.selectNodes(doc);
System.out.println("某班共有"+userList.size()+"个学生");
for(Iterator it=userList.iterator();it.hasNext();){
Element userElement=(Element)it.next();
String id=userElement.getAttributeValue("id");
String name=userElement.getChildText("name");
//修改龙准的年龄
if(name.equals("龙准")){
userElement.getChild("age").setText("26");
}
String age=userElement.getChild("age").getTextTrim();
System.out.println("学生 "+id);
System.out.println("姓名:"+name+" 年龄:"+age);
}
//保存到硬盘
Format fm=Format.getCompactFormat();
fm.setEncoding("gb2312");
fm.setIndent(" ");
XMLOutputter out=new XMLOutputter(fm);
out.output(doc, new FileOutputStream("d://user2.xml"));
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}