String xpath = String format("/root/shools/shool[@id=\"%d\"]",xx);
String xpath = "/root/shools";
String xpath = "/root/shools/shool[@id=1]";
String xpath = "//root/shools/shool/class/student[@name=\"alice\"]";
1.获得文档
a.新的
Document document = DocumentHelper.createDocument();
b.旧的
InputStreamReader read=new InputStreamReader(new FileInputStream(filePath), “UTF8”);
SAXReader reader = new SAXReader();
Document document = reader.read(read);
2.查找document单个节点
Node node = document.selectSingleNode(xpath);
3.查找document子节点列表
List<Node> list = document.selectNodes(xpath);
4.写文档
OutputFormat format = null;
format = OutputFormat.createCompactFormat(); 紧凑格式
format = OutputFormat.createPrettyPrint(); 非紧凑格式
format.setTrimText(false); ??
format.setPadText(false); ??
format.setEncoding("GB2312");//用于支持中文
XMLWriter writer = new XMLWriter(new FileWriter(savePath), format);
writer.write( document );
writer.close();
<也能支持中文>
OutputFormat format = OutputFormat.createPrettyPrint(); 非紧凑格式
FileOutputStream fos = new FileOutputStream(savePath);
XMLWriter writer = new XMLWriter(fos, format);
writer.write(document);
writer.close();
5. 增加根节点
Element root = document.addElement(“root”);
6.新增加元素
Element shoolElement = root.addElement("school");
7.增加属性
shoolElement.addAttribute("shoolName", "xxxx学校");
7.设置元素文本???
element.setText("sdfsdfs");
9.查找node下的单个节点
xpath = String.format("/root/shools/shool/class[@classid=\"%d\"]",1);
Node selNode = node.selectSingleNode(xpath);
10.查找node下的多个节点
List<Node> list = node.selectNodes(xpath);
11. 取得节点属性值
String attribute = node.valueOf("@属性名"); 必加@
12.取得Element属性值
element.attributeValue(attribute_name);
13. 修改属性值
Attribute attribute = element.attribute(attribute_name);
if (attribute != null)
attribute.setValue(attribute_value);
14.删除节点
if (node.getParent() == null)
document.remove(node);
else node.getParent().remove(node);
15.判断字符串是否是合法的XML
DocumentHelper.parseText(content);
String xpath = "/root/shools";
String xpath = "/root/shools/shool[@id=1]";
String xpath = "//root/shools/shool/class/student[@name=\"alice\"]";
1.获得文档
a.新的
Document document = DocumentHelper.createDocument();
b.旧的
InputStreamReader read=new InputStreamReader(new FileInputStream(filePath), “UTF8”);
SAXReader reader = new SAXReader();
Document document = reader.read(read);
2.查找document单个节点
Node node = document.selectSingleNode(xpath);
3.查找document子节点列表
List<Node> list = document.selectNodes(xpath);
4.写文档
OutputFormat format = null;
format = OutputFormat.createCompactFormat(); 紧凑格式
format = OutputFormat.createPrettyPrint(); 非紧凑格式
format.setTrimText(false); ??
format.setPadText(false); ??
format.setEncoding("GB2312");//用于支持中文
XMLWriter writer = new XMLWriter(new FileWriter(savePath), format);
writer.write( document );
writer.close();
<也能支持中文>
OutputFormat format = OutputFormat.createPrettyPrint(); 非紧凑格式
FileOutputStream fos = new FileOutputStream(savePath);
XMLWriter writer = new XMLWriter(fos, format);
writer.write(document);
writer.close();
5. 增加根节点
Element root = document.addElement(“root”);
6.新增加元素
Element shoolElement = root.addElement("school");
7.增加属性
shoolElement.addAttribute("shoolName", "xxxx学校");
7.设置元素文本???
element.setText("sdfsdfs");
9.查找node下的单个节点
xpath = String.format("/root/shools/shool/class[@classid=\"%d\"]",1);
Node selNode = node.selectSingleNode(xpath);
10.查找node下的多个节点
List<Node> list = node.selectNodes(xpath);
11. 取得节点属性值
String attribute = node.valueOf("@属性名"); 必加@
12.取得Element属性值
element.attributeValue(attribute_name);
13. 修改属性值
Attribute attribute = element.attribute(attribute_name);
if (attribute != null)
attribute.setValue(attribute_value);
14.删除节点
if (node.getParent() == null)
document.remove(node);
else node.getParent().remove(node);
15.判断字符串是否是合法的XML
DocumentHelper.parseText(content);