sax解析
**********************************************************************************
package test;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.io.*;
public class SaxTest extends DefaultHandler {
// 重载DefaultHandler类的方法
// 以拦截SAX事件通知。
//
// 关于所有有效事件,见org.xml.sax.ContentHandler
//
public void startDocument() throws SAXException {
//System.out.println( "SAX Event: START DOCUMENT" );
}
public void endDocument() throws SAXException {
//System.out.println( "SAX Event: END DOCUMENT" );
}
public void startElement(String namespaceURI,
String localName,
String qName,
Attributes attr) throws SAXException {
System.out.print("<" + localName);
// 如果有属性,我们也一并打印出来...
for (int i = 0; i < attr.getLength(); i++) {
System.out.print(" " + attr.getLocalName(i) + "='" +attr.getValue(i)+"'");
}
System.out.print(">");
}
public void endElement(String namespaceURI,
String localName,
String qName) throws SAXException {
System.out.print("" ? + localName> }
public void characters(char[] ch, int start, int length) throws
SAXException {
try {
OutputStreamWriter outw = new OutputStreamWriter(System.out);
outw.write(ch, start, length);
outw.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] argv) {
System.out.println("Example1 SAX Events:");
try {
// SAXParserFactory spFactory = SAXParserFactory.newInstance();
// SAXParser sParser = spFactory.newSAXParser();
// 建立SAX 2解析器...
XMLReader xr = XMLReaderFactory.createXMLReader(
"org.apache.xerces.parsers.SAXParser");
// 安装ContentHandler...
xr.setContentHandler(new SaxTest());
// 解析文件...
xr.parse(new InputSource(
new FileReader("c:\\DeviceTable.xml")));
} catch (Exception e) {
e.printStackTrace();
}
}
}
JDOM解析
***********************************************************************************
package test;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import java.util.List;
import java.util.*;
/**
* @author kingwong
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class TestJDOM2 {
public static void main(String[] args){
SAXBuilder sb = new SAXBuilder();
try
{
Document doc = sb.build("c:\\DeviceTable.xml");
Element root = doc.getRootElement();
List list=root.getChildren();
getchildren(list);
String str1 = root.getAttributeValue("comment");
System.out.println("Root Element's comment attribute is : " + str1);
// String str2 = root.getChild("NeCode").getAttributeValue("sss");
// System.out.println("NeCode Element's sss attribute is : " + str2);
// String str3 = root.getChildText("name");
// System.out.println("name Element's content is :" + str3);
// String str4 = root.getChild("contact").getChildText("telephone");
// System.out.println("contact Element's telephone subelement content is : " + str4 + "\n");
// Element inputElement = root.getChild("contact");
// inputElement.addContent(new Element("email").setAttribute("value","wanghua@cyberobject.com"));
XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
// String outStr = xmlOut.outputString(root);
// System.out.println(outStr);
}
catch(Exception e)
{
e.printStackTrace();
}
}
private static void getchildren(List list) {
Iterator iter = list.iterator();
while (iter.hasNext()) {
Element item = (Element) iter.next();
System.out.println("***<"+item.getName()+">"+item.getValue()+""+ITEM.GETNAME()+">");
if(item.getChildren().size()!=0){
getchildren(item.getChildren());
}else{
// System.out.println("###"+item.getName()+"没有子节点!!");
}
}
}
}