XML:Extensible Markup Language
XML:常用作软件配置文件,描述模块之间关系;
XML:文档声明,元素,属性,注释,CDATA区,特殊字符,处理指令
声明:<?xml version=”1.0” encoding=”gb2312” ?>
空格换行都被当原始内容处理
一个标签有多个属性,写作 这个也可写为
<input>
<name>text</name>
</input>
注释要写在声明之后
CDATA区
<![CDATA[
<input>
<name>text</name>
</input>
]]
处理指令: <?xml-stylesheet type=”text/css” href=”config.css”?>
XML约束,编写一个文档约束一个XML文档的书写规范; XML DTD XML Schema —
<!DOCTYPE 书架 SYSTEM "book.dtd">
<书架>
<书>
<书名></书名>
<作者></作者>
<售价></售价>
</书>
</书架>
//dtd可以存在与xml中
book.dtd:
<!ELEMENT 书架(书+)>
<!ELEMENT 书(书名,作者,售价)>
<!ELEMENT 书名(#PCDATA)>
<!ELEMENT 作者(#PCDATA)>
<!ELEMENT 售价(#PCDATA)>
属性定义 <!ATTLIST 页面作者 姓名 CDATA #IMPLIED 年龄 CDATA #IMPLIED 联系方式 CDATA #REQUIRED 网站职务 CDATA #FIXED “页面作者” 个人爱好 CDATA “上网”
XML编程(CRUD)create read update delete
XML解析方式:dom,sax dom W3C推荐 sax XML社区事实的标准 — sax解析: 从上往下读,读取一行处理一行,内存消耗小,但不适合增删改 — CRUD
DocumentBuilderFactory factory = DocumentBuliderFactory.Instance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("src/book.xml");
//查
Node root = document.getElementsByTagName("书架").item(0);
if(node instanceof Element){
system.out.print(node.getNodeName());
}
//增(默认增加到最后)
Element price = document.createElement("售价");
price.setTextContent("59.00元");
Element book = document.getElementsByTagName("书").item(0);
book.appendChild(pirce);
//增(增加到某个节点后)
//1.得到参考节点
Element rfNode = (ELement)document.getElementsByTagName("售价").item(0);
//2.得到要挂载的节点
Element book = document.getElementsByTagName("书").item(0);
book.appendChild(pirce);
//3.插入
book.insertBefore(newChild,rfNode);
//增加属性
Element bookname = document.getElementsByTagName("书名").item(0);
bookname.setAttribute("name","xxxx");
//删除节点
Element e = document.getElementsByTagName("售价").item(0);
Element book = document.getElementsByTagName("书").item(0);
book.removeChild(e);
//或者
e.getParentNode().removeChild(e);
//把更新后内存写回xml文档
TransformerFactory tffactory = TransFormerFactory.newInstance();
Transform tf = tffactory.newTransformer();
tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/book.xml"));
命名方式:
UI:user interface
dao
domain/entity/bean 封装数据的实体
Utils getDocument(),write2Xml(Document document)
使用Dom4j解析XML — 增
Document document = DocumentHelper.createDocument();
Element root = document.addElement("root");
Element author1 = root.addElement("author")
.addAttribute("name","James")
.addAttribute("location","James")
.addText("James Strachan")
return document;
写入xml
XMLWriter writer = new XMLWriter(
new FileWriter("output.xml")
);
wirter.write(document);
writer.close;
//Pretty print the document to System.out
OutputFormat format = OutputFormat.createPrettyPrint();
writer = new XMLWriter(System.out,format);
writer.write(document);
读取XML
SAXReader reader = new SAXReader();
Document document = reader.reader(new File("src/book.xml"))
Element root = document.getRootElement();
root.element("书").get(1);
乱码问题
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),format)
writer.write(document);
writer.close();