微科社区,轻松开发从此开始! 请登陆 免费注册

微科社区

当前位置:首页 > Java平台 > J2EE >

java中四种操作(dom、sax、jdom、dom4j)xml方(3)

时间:2017-01-11 04:01  浏览:努力统计中...
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,Attribu
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);}
}

3) JDOM

 

import java.io.*; 
import java.util.*;
import org.jdom.*;
import org.jdom.input.*;

public class MyXMLReader {

 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());
   }
  } catch (Exception e) {
   e.printStackTrace();
}

}

4)DOM4J

import java.io.*; 
import java.util.*;
import org.dom4j.*;
import org.dom4j.io.*;

public class MyXMLReader {

 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"));
   }
  } catch (Exception e) {
   e.printStackTrace();
}
)

 

 

标签: xml, dom4j, sax, java
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线------
栏目列表
推荐内容