1 test.xml
<?xml version="1.0" encoding="GB2312"?>
<RESULT>
<VALUE>
<NO>A1234</NO>
<ADDR>四川省XX县XX镇XX路X段XX号</ADDR>
</VALUE>
<VALUE>
<NO>B1234</NO>
<ADDR>四川省XX市XX乡XX村XX组</ADDR>
</VALUE>
</RESULT>
<RESULT>
<VALUE>
<NO>A1234</NO>
<ADDR>四川省XX县XX镇XX路X段XX号</ADDR>
</VALUE>
<VALUE>
<NO>B1234</NO>
<ADDR>四川省XX市XX乡XX村XX组</ADDR>
</VALUE>
</RESULT>
2
/**//*
* MyXMLReader.java
*
* Created on 2008年2月24日, 下午8:46
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package XmlDom;
import javax.xml.parsers.*;
import java.io.*;
import java.util.*;
import org.w3c.dom.*;
/** *//**
*
* @author rulinma
*/
public class MyXMLReader ...{
/** *//** Creates a new instance of MyXMLReader */
public MyXMLReader() ...{
}
public static void main(String arge[])...{
long lasting = System.currentTimeMillis();
try
...{
File f = new File("d:/test.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
NodeList nl = doc.getElementsByTagName("VALUE");
for (int i=0; i< nl.getLength(); i++)
...{
System.out.print("车牌号码:" + doc.getElementsByTagName("NO").item(i).getFirstChild().getNodeValue());
System.out.println("车主地址:" + doc.getElementsByTagName("ADDR").item(i).getFirstChild().getNodeValue());
}
}
catch(Exception e)
...{
e.printStackTrace();
}
}
}
* MyXMLReader.java
*
* Created on 2008年2月24日, 下午8:46
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package XmlDom;
import javax.xml.parsers.*;
import java.io.*;
import java.util.*;
import org.w3c.dom.*;
/** *//**
*
* @author rulinma
*/
public class MyXMLReader ...{
/** *//** Creates a new instance of MyXMLReader */
public MyXMLReader() ...{
}
public static void main(String arge[])...{
long lasting = System.currentTimeMillis();
try
...{
File f = new File("d:/test.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
NodeList nl = doc.getElementsByTagName("VALUE");
for (int i=0; i< nl.getLength(); i++)
...{
System.out.print("车牌号码:" + doc.getElementsByTagName("NO").item(i).getFirstChild().getNodeValue());
System.out.println("车主地址:" + doc.getElementsByTagName("ADDR").item(i).getFirstChild().getNodeValue());
}
}
catch(Exception e)
...{
e.printStackTrace();
}
}
}
运行结果:
init:
deps-jar:
compile-single:
run-single:
车牌号码:A1234车主地址:四川省XX县XX镇XX路X段XX号
车牌号码:B1234车主地址:四川省XX市XX乡XX村XX组
生成成功(总时间:0 秒)
SAX方法:
/**//*
* XMLSax.java
*
* Created on 2008年2月24日, 下午9:14
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package XmlDom;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;
/** *//**
*
* @author rulinma
*/
public class XMLSax extends DefaultHandler ...{
java.util.Stack tags = new java.util.Stack();
/** *//** Creates a new instance of XMLSax */
public XMLSax() ...{
super();
}
public static void main(String args[])
...{
long lasting = System.currentTimeMillis();
try
...{
SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sp = sf.newSAXParser();
XMLSax reader = new XMLSax();
sp.parse(new InputSource("d:/test.xml"), reader);
}
catch (Exception e)
...{
e.printStackTrace();
}
System.out.println("运行时间:" + (System.currentTimeMillis() - lasting) + "毫秒");
}
public void characters(char ch[], int start, int length) throws SAXException
...{
String tag = (String) tags.peek();
if (tag.equals("NO"))
...{
System.out.print("车牌号码:" + new String(ch, start, length));
}
if (tag.equals("ADDR"))
...{
System.out.println("地址:" + new String(ch, start, length));
}
}
public void startElement(String uri,String localName,String qName,Attributes attrs)
...{
tags.push(qName);
}
}
* XMLSax.java
*
* Created on 2008年2月24日, 下午9:14
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package XmlDom;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;
/** *//**
*
* @author rulinma
*/
public class XMLSax extends DefaultHandler ...{
java.util.Stack tags = new java.util.Stack();
/** *//** Creates a new instance of XMLSax */
public XMLSax() ...{
super();
}
public static void main(String args[])
...{
long lasting = System.currentTimeMillis();
try
...{
SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sp = sf.newSAXParser();
XMLSax reader = new XMLSax();
sp.parse(new InputSource("d:/test.xml"), reader);
}
catch (Exception e)
...{
e.printStackTrace();
}
System.out.println("运行时间:" + (System.currentTimeMillis() - lasting) + "毫秒");
}
public void characters(char ch[], int start, int length) throws SAXException
...{
String tag = (String) tags.peek();
if (tag.equals("NO"))
...{
System.out.print("车牌号码:" + new String(ch, start, length));
}
if (tag.equals("ADDR"))
...{
System.out.println("地址:" + new String(ch, start, length));
}
}
public void startElement(String uri,String localName,String qName,Attributes attrs)
...{
tags.push(qName);
}
}