建体彩网|中彩网双色球连号|
?
快捷搜索:  as  test  1111  test aNd 8=8  test++aNd+8=8  as++aNd+8=8  as aNd 8=8

皇馬國際app:Java讀取xml文件的四種方法

?

xml文件:

Xml代碼

A1234

河南省鄭州市

B1234

河南省鄭州市二七區

第一種 DOM 實現措施:

Java代碼

import java.io.File;

import javax.xml.parsers皇馬國際app.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import org.w3c.dom.NodeList;

public class MyXMLReader2DOM {

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皇馬國際app.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("車主地址:" + foo.elementText("ADDR"));

}

System.out.println("運行光陰:" + (System.currentTimeMillis() - lasting)

+ "毫秒");

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

第三種 JDOM實現措施:

Java代碼

import java.io.*;

import java.util.*;

impo皇馬國際apprt org.jdom.*;

import org.jdom.input.*;

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("AD皇馬國際appDR").getText());

}

System.out.println("運行光陰:" + (System.currentTimeMillis() - lasting)

+ "毫秒");

}

} 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"), 皇馬國際appreader);

} 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);

}

}

免責聲明:以上內容源自網絡,版權歸原作者所有,如有侵犯您的原創版權請告知,我們將盡快刪除相關內容。

您可能還會對下面的文章感興趣:

建体彩网
七星彩玩法 福建36选7 老时时彩app 秒速飞艇正规吗 香港管家婆六合图库 极速快3和值规律 竞彩足球比分推荐预测 山水云南麻将麻将 重庆快乐十分遗漏任五 双色球现场直播