认识XML
可以方便实现 数据交换 系统配置 内容管理
前导区 定义一些信息:version encoding standalone (是否独立运行)
<?xml version="1.0" encoding="GB2312" standalone="no"?>
<?xml-stylesheet type="text/css" href="attrib.css">
按照css文本显示
由于“<”,">",有特殊含义,为了正确显示 提供了五个实体参照
No. | 实体参照 | 对应字符 |
1 | & | & |
2 | < | < |
3 | > | > |
4 | " | " |
5 | &apos | ' |
DOM解析
DOM 接口机制(随机访问机制)
优点:随机访问、DOM分析器采用树的结构与XML文档相吻合
缺点:当文档比较大时,对内存的要求比较高
package a;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder ;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import java.lang.Object;
public class DOMdemo2 {
public static void main(String arg[]) {
javax.xml.parsers.DocumentBuilderFactory factory=javax.xml.parsers.DocumentBuilderFactory.newInstance();
javax.xml.parsers.DocumentBuilder builder=null;
// System.setProperty( "javax.xml.parsers.DocumentBuilderFactory","org.apache.crimson.jaxp.DocumentBuilderFactoryImpl" );
try {
builder=factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Document doc=null;
try {
System.out.println();
doc=builder.parse("E:\\jee-oxygen\\file2\\webFile_2\\dom_demo_01.xml");
System.out.println(doc);
} catch (SAXException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
NodeList nl=doc.getElementsByTagName("linkman");
System.out.println("aaaaaaaaaaaaaaaa");
for(int x=0;x<nl.getLength();x++) {
Element e=(Element) nl.item(x);
System.out.println("姓名:"+e.getElementsByTagName("name").item(0).getFirstChild().getNodeName());
System.out.println("aaaaaaaaaaaaaaaa");
System.out.println("邮箱:"+e.getElementsByTagName("email").item(0).getFirstChild().getNodeValue());
}
Document doc1=null;
doc1=builder.newDocument();
Element addresslist=doc1.createElement("addresslist");
Element linkman=doc1.createElement("linkman");
Element name=doc1.createElement("name");
Element email=doc1.createElement("email");
name.appendChild(doc1.createTextNode("aijuntao"));
email.appendChild(doc1.createTextNode("3939393@111.com"));
linkman.appendChild(name);
linkman.appendChild(email);
addresslist.appendChild(linkman);
doc1.appendChild(addresslist);
TransformerFactory tf=TransformerFactory.newInstance();
Transformer t=null;
try {
t=tf.newTransformer();
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t.setOutputProperty(OutputKeys.ENCODING, "GBK");
DOMSource source=new DOMSource(doc1);
StreamResult result=new StreamResult(new File("E:\\jee-oxygen\\file2\\webFile_2\\dom_demo_02.xml"));
try {
t.transform(source, result);
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
SAX 解析:
package a;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.junit.Test;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SAXdemo extends DefaultHandler{
public void startDocument()throws SAXException{
//文档开始时执行
}
public void endDocument()throws SAXException{
//文档结束执行
}
public void startElement(String uri,String localname,String name,Attributes attributes) throws SAXException{
System.out.println(" 这是元素开始:");
System.out.println("uri是什么"+uri);
System.out.println("localname是什么"+uri);
}
public void characters(char []ch,int start,int longth) throws SAXException {
//这个是元素的内容
System.out.println(new String(ch,start,longth));
}
public void endElement(String uri,String localname,String name) throws SAXException{
System.out.println(" 这是元素开始:");
System.out.println("localname是什么"+uri);
}
@Test
public void ttt() throws ParserConfigurationException, SAXException, IOException {
SAXParserFactory factory=SAXParserFactory.newInstance();
SAXParser parser=factory.newSAXParser();
parser.parse("E:\\jee-oxygen\\file2\\webFile_2\\dom_demo_02.xml", new SAXdemo());
}
}
JDOM
优点=DOM 优点 +SAX 优点=DOM修改文件的优点+SAX读取快速的优点
JDOM 生成XML
org.jdom.Attribute;
org.jdom.Element;
org.jdom.Document;
org.jdom.output.XMLOutputter
Element addresslist =new Element("addresslist");
Document doc=new Document(addresslist);
XMLOutputter out=new XMLOutputter();
out.setFormat(out.getFormat().setEncoding("GBK"));
out.output(doc,new FileOutputStream(filepath));
JDOM 读取 XML文件
org.jdom.Document;
org.jdom.Element;
org.jdom.input.SAXBuilder;
SAXBulder builder=new SAXBuilder();
Document doc=builder.builder("filepath");
Element stu=doc.getRootELement();
List list=stu.getChildern("linkman")
for(int i =0; i<list.length;i++)
{
Element e=(Element)list.get(i);
String name=e.getChildText("name");
}
DOM4J 与 JDOM非常类似