XML Notebook
XML: EXtensible Markup Language
无论text写什么,都当作普通文本
DOM4J works with DOM, SAX, XPath, and XSLT. It can parse large XML documents with very low memory footprint.
1. Basic Syntax
1.1 Processing instruction
<? ... ?> format- e.g.
<?xml version= "1.0" encoding= "utf-8" ?>
Must have:
- version
Optional:
- standalone: yes/no : 不使用外部声明
- encoding: utf-8/gbk ...
1.2 element & attribute
- element: <tag attribute="..." ...>...</tag>
- 属性:单双引号皆可
1.3 大小写敏感
- HTML:大小写不敏感
1.4 必须有根元素
1.5 实体引用
实体:任何包含数据的项实体中要使用转义字符:
< | < |
> | > |
& | & |
' | ' |
" | " |
1.6 CDATA block
format: <![CDATA [ text ]]>无论text写什么,都当作普通文本
2. Parsers
2.1 SAX & DOM
SAX: simple API for XML
- Pros: 逐行扫描,边扫描边解析,速度快
- Cons: 不能对节点进行修改
DOM: Document Object Model
- pros: 构造Tree,方便遍历和修改
- cons: 对于大文件,内存压力大
- DOM4J:
DOM4J works with DOM, SAX, XPath, and XSLT. It can parse large XML documents with very low memory footprint.
3. DOM4J
3.1 SAXReader:
SAXReader reader = new SAXReader(); Document doc = reader.read(new File(filename));
3.2 Document
Element root = doc.getRootElement();
3.3 Element
- Element element(String name); // 获取name子元素
- List<Element> elements();
- String getName();
- String getText();
- Attribute attribute(int index / String name);
3.4 Attribute
- String getName();
- String getValue();
3.5 Write
3.5.1 Create Document
Document DocumentHelper.createDocument(); Document的addElement():只能调用一次 Element root = doc.addElement("project");
3.5.2 Element
- addElement(String name);
- addAttribute(String name, String val);
- addText(String txt);
3.5.3 Output
XMLWriter writer = new XMLWriter(); FileOutputStream fos = new FileOutputStream('new.xml'); writer.setOutputStream(fos); writer.write(doc); writer.close();
3.6 XPath
- absolute path: starts with /
- relative path: step/step
. | current node |
.. | the parent of current node |
/ | Selection starts from the root node |
// | 任意位置的某个节点 |
@ | 选择属性 |
Predicates:
[predicates]/books/book[1] | 选择第一个book子元素 |
/books/book[last()] | 选择最后一个book子元素 |
/books/book[@lang] | 选择有lang属性的子元素 |
/books/book[@lang='eng'] | 选择有lang属性为eng的子元素 |
/books/book[price] | 选择有price子元素的book |
/books/book/price[.>35.00] | 选择大于35的/books/book/的price子元素 |
通配符:
* | 任何元素 |
@* | 任何属性 |
node() | 任何类型的节点 |
//* | 所有元素 |
/*/* | 所有第二层元素 |
DOM4J对XPath的支持:
List list = doc.selectNodes("...");
相关推荐
chensen 2020-11-14
lwnylslwnyls 2020-11-06
ATenhong 2020-10-15
yanzhelee 2020-10-13
佛系程序员J 2020-10-10
guojin0 2020-10-08
佛系程序员J 2020-10-08
bluewelkin 2020-09-16
wwzaqw 2020-09-04
zhongdaowendao 2020-09-02
favouriter 2020-08-18
奎因amp华洛 2020-08-15
一青年 2020-08-13
千锋 2020-08-10
nangongyanya 2020-08-09
dongxurr 2020-08-08
明天你好 2020-08-03
kyelu 2020-08-03
Ashes 2020-08-03