xml
XML 可扩展标记语言 ,是一种简单的数据存储语言,使用一系列简单的标签描述数据,使人们或者程序可以理解 ; 可读性强 灵活性高.
对44.txt文件中的内容进行提取,在这里提取Weather中所包含的内容
import java.io.File;
import java.io.IOException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.w3c.dom.NamedNodeMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Node;
public class FileXml {
public static void main(String[] args) {
File file =new File ("d:\\44.txt");
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db=dbf.newDocumentBuilder();
Document doc=db.parse(file);
NodeList weathers=doc.getElementsByTagName("Weather");
for (int i = 0; i < weathers.getLength(); i++) {
Node weather =weathers.item(i);
/* NamedNodeMap map=weather.getAttributes(); System.out.println(map.getNamedItem(" ").getNodeValue());*/
for(Node node =weather.getFirstChild();node!=null;node=node.getNextSibling())
if(node.getNodeType()==node.ELEMENT_NODE){
System.out.println(node.getNodeName());
if(node.getFirstChild()!=null){
System.out.println(node.getFirstChild().getNodeValue());
}
}
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行结果:
SAX
自己定义的一个文件解析器,重写了默认的方法
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class MySAXHandle extends DefaultHandler{
@Override
public void characters(char[] ch,int start,int length) throws SAXException {
// TODO Auto-generated method stub
super.characters(ch,start,length);
System.out.println("标签内容 is "+new String(ch,length));
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
System.out.println("文档结束");
}
@Override
public void endElement(String uri,String localName,String qName) throws SAXException {
// TODO Auto-generated method stub
super.endElement(uri,localName,qName);
System.out.println("标签结束");
}
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
System.out.println("文档开始 ");
}
@Override
public void startElement(String uri,String qName,Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
super.startElement(uri,qName,attributes);
System.out.println("标签开始 name is "+qName);
}
}
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
public class FileSax {
public static void main(String[] args) {
File file=new File("d:\\44.txt");
SAXParserFactory factory=SAXParserFactory.newInstance();
try {
SAXParser parser =factory.newSAXParser();
MySAXHandle handle=new MySAXHandle();
parser.parse(file,handle);
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行结果为: