1.schema约束
格式:<schema xmlns="http://www.w3.org/2001/XMLSchema" //表示文件是约束文件
targetNamespace="http://www.example.org/1" //通过空间地址引用约束
elementFormDefault="qualified">
<complexType>
<sequence>//元素出现的顺序,如果是all标签,表示只出现一次,若是choice标签表示只能出现一个
<any></any maxOccurs="unbounded">//表示任意元素,maxOccurs属性表示出现次数不限
</sequence>
</complexType>
</schema>
2.jaxp的sax解析
例子:
public static void main(String[] args){
SAXParserFactory sax = SAXParseFactory.newInstance();
SAXParser sp = sax.newSAXParser();
sp.parse("解析文件路径",new MyDefault());
}
class MyDefault extends DefaultHandlers{
重写startElement(),endElement(),characters()三个方法
}
3.使用dom4j添加标签
SAXReader sr = new SAXReader();
Document document = sr.read("解析文件路径");
Element root = document.getRootNode();
Element ele = root.addElement("name");
ele.setText("zhangsan");
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter xw = new XMLWriter(new FileOutputStream("解析文件的路径"),format);
xw.write(document);
xw.close();
4.使用dom4j修改节点内容
SAXReader sr = new SAXReader();
Document document = sr.read("解析文件路径");
Element root = document.getRootNode();
Element info = root.element("info");
Element name = info.element("name");
name.setText("要修改的内容");
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter xw = new XMLWriter(new FileOutputStream("解析文件的路径"),format);
xw.write(document);
xw.close();
5.使用dom4j删除节点
SAXReader sr = new SAXReader();
Document document = sr.read("解析文件的路径");
Element root = document.getRootNode();
Element info = root.element(info);
Element name = info.element("name");
info.remove(name);
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter xw = new XMLWriter(new FileOutputStream("解析文件的路径"),format);
xw.write(document);
xw.close();
6.使用dom4j获取属性
SAXReader sr = new SAXReader();
Document document = sr.read("解析文件的路径");
Element root = document.getRootNode();
Element info = root.element("info");
Element name = info.element("name");
String id = name.attributeValue("id");
7.使用dom4j支持xpath操作可以直接获取到某个元素
xpath六种形式:
/AAA/BBB/CCC :一层一层的,AAA下面BBB的CCC
//BBB :只要名称是BBB的都得到
/* :得到所有元素
/A/BBB[1] :A下的第一个BBB元素
/AAA/BBB[last()] :AAA下的最后一个BBB元素
//@id :所有含id属性的元素
//BBB[@id] :所有含有id属性的BBB元素
//BBB[@id='1'] :id属性值为1的所有BBB元素
8.dom4j和xpath结合的方法
.selectNodes("xpath表达式");
.selectSingleNode("xpath表达式");
9.使用xpath实现查询xml中所有name元素的值
SAXReader sr = new SAXReader();
Document document = sr.read("解析文件路径");
List<node> list = document.selectNodes("//name");
for(Node node:list){
String str = node.getText();
System.out.println(str);
}
10.使用xpath的增加操作(插入增加节点,非在最后增加节点)
SAXReader sr = new SAXReader();
Document document = sr.read("解析文件路径");
List<Node> list = document.selectNodes("info");
Element info = DocumentHelper.createElement("info");
Element name = info.addElement("name");
name.setText("zhangsan");
Element age = info.addElement("age");
age.setText("22");
list.add(1,info);
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter xw = new XMLWriter(new FileOutputStream("解析文件路径",format));
xw.write(document);
xw.close();
11.删除,查询操作类似
与增加不同需要根据得到的子标签获得父标签
ele.getParent();