快捷搜索:  as  test  1111  test aNd 8=8  test++aNd+8=8  as++aNd+8=8  as aNd 8=8

ag旗舰厅手机版:Java读取xml文件的四种方法



xml文件:

Xml代码

A1234

河南省郑州市

B1234

河南省郑州市二七区

第一种 DOM 实现措施:

Java代码

import java.io.File;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import org.w3c.dom.NodeList;

public clag旗舰厅手机版ass MyXMLReader2ag旗舰厅手机版DOM {

public static void main(String arge[]) {

long lasting = System.currentTimeMillis();

try {

File f = new File("data_10k.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());

System.out.println("运行光阴:" + (System.currentTimeMillis() - lasting)

+ "毫秒");

}

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

第二种,DOM4J实现措施:

Java代码

import java.io.*;

import java.util.*;

import org.dom4j.*;

import org.dom4j.io.*;

public class MyXMLReader2DOM4J {

public static void main(String arge[]) {

long lasting = System.currentTimeMillis();

try {

File f = new File("data_10k.xml");

SAXReader reader = new SAXReader();

Document doc = reader.read(f);

Element root = doc.getRootElement();

Element foo;

for (Iterator i = root.elementIterator("VALUE"); i.hasNext();) {

foo = (Element) i.next();

System.out.print("车商标码:" + foo.elementText("NO"));

System.out.println("车主地ag旗舰厅手机版址:" + foo.elementText("ADDR"));

}

System.out.println("运行光阴:" + (System.currentTimeMillis() - lasting)

+ "毫秒");

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

第三种 JDOM实现措施:

Java代码

import java.io.*;

import java.util.*;

import org.jdom.*;

import org.jdom.inpuag旗舰厅手机版t.*;

public class MyXMLReader2JDOM {

public static void main(String arge[]) {

long lasting = System.currentTimeMillis();

try {

SAXBuilder builder = new SAXBuilder();

Document doc = builder.build(new File("data_10k.xml"));

Element foo = doc.getRootElement();

List allChildren = foo.getChildren();

for (int i = 0; i < allChildren.size(); i++) {

System.out.print("车商标码:"+ ((Element) allChildren.get(i)).getChild("NO").getText());

System.out.println("车主地址:"+ ((Element) allChildren.get(i)).getChild("ADDR").getText());

}

System.out.println("运行光阴:" + (System.currentTimeMillis() - lasting)

+ "毫秒ag旗舰厅手机版");

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

第四种SAX实现措施:

Java代码

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;

import org.xml.sax.InputSource;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

public class MyXMLReader2SAX extends DefaultHandler {

java.util.Stack tags = new java.util.Stack();

public MyXMLReader2SAX() {

super();

}

public static void main(String args[]) {

long lasting = System.currentTimeMillis();

try {

SAXParserFactory sf = SAXParserFactory.newInstance();

SAXParser sp = sf.newSAXParser();

MyXMLReader2SAX reader = new MyXMLReader2SAX();

sp.parse(new InputSource("data_10k.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);

}

}

免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。

您可能还会对下面的文章感兴趣: