这是一个采用sax方式读取xml的程序:import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;import java.util.*;
import java.io.*;
public class SAXTagCount extends DefaultHandler {
static String path="";
static String tag=".";
public void startDocument() throws SAXException {
}
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts)
throws SAXException{
if(path.equals(""))
path=qName;
else
path=path+tag+qName;
} public void endDocument() throws SAXException {
} public void characters(char[] ch, int start, int length){
System.setProperty(path,new String(ch,start,length));
} public void endElement(String uri, String localName, String qName){
if(path.length()==qName.length())
path="";
else
path=path.substring(0,path.length()-((qName+tag).length()));
}
static public void main(String[] args) {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setValidating(true);
XMLReader xmlReader = null;
try {
SAXParser saxParser = spf.newSAXParser();
// xmlReader = saxParser.getXMLReader();
saxParser.parse(new File(args[0]),new SAXTagCount());
} catch (Exception ex) {
System.err.println(ex.toString());
}
//xmlReader.setContentHandler(new SAXTagCount());
System.getProperties().list(System.out);
}
}
运行环境 jdk1.4 你看看对你有没有帮助祝你好运!
import org.xml.sax.*;
import org.xml.sax.helpers.*;import java.util.*;
import java.io.*;
public class SAXTagCount extends DefaultHandler {
static String path="";
static String tag=".";
public void startDocument() throws SAXException {
}
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts)
throws SAXException{
if(path.equals(""))
path=qName;
else
path=path+tag+qName;
} public void endDocument() throws SAXException {
} public void characters(char[] ch, int start, int length){
System.setProperty(path,new String(ch,start,length));
} public void endElement(String uri, String localName, String qName){
if(path.length()==qName.length())
path="";
else
path=path.substring(0,path.length()-((qName+tag).length()));
}
static public void main(String[] args) {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setValidating(true);
XMLReader xmlReader = null;
try {
SAXParser saxParser = spf.newSAXParser();
// xmlReader = saxParser.getXMLReader();
saxParser.parse(new File(args[0]),new SAXTagCount());
} catch (Exception ex) {
System.err.println(ex.toString());
}
//xmlReader.setContentHandler(new SAXTagCount());
System.getProperties().list(System.out);
}
}
运行环境 jdk1.4 你看看对你有没有帮助祝你好运!
解决方案 »
- JWindow 点击其他地方 窗口自动消失
- 程序可以运行,可是当点击"open"按钮时候,系统有报错了,求高手解答
- @@@@@@@@@算法问题,求多边形的重合部分面积
- 是线程的问题:大家都知道继承Thread中必须要重载run的...
- 用JAVA能不能做个象QQ那样的界面。(主要是:“我的好友”,“陌生人”之间的转换。)
- 读写锁原理
- 请问:运算符的小问题??
- 请问在java里如何设置背景图片?
- 如果在synchronized块内的代码是一循环语句,那其它的synchronized方法是如何响应的!
- 小弟我正想学习Java,打算使用JBuilder,不知哪位前辈肯介绍一下JBuilder,用的人多不多?
- How can I use GJ compiler with jdk1.4.1
- 今天升到5个裤衩,高兴,散分
http://www.cafeconleche.org/books/xmljava/这又一本树
就写的这个,里面有你要的!
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;import java.util.*;
import java.io.*;
public class SAXTagCount extends DefaultHandler {
static String path="";
static String tag=".";
public void startDocument() throws SAXException {
}
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts)
throws SAXException{
if(path.equals(""))
path=qName;
else
path=path+tag+qName;
} public void endDocument() throws SAXException {
} public void characters(char[] ch, int start, int length){
System.out.println(path+"="+new String(ch,start,length).trim());
} public void endElement(String uri, String localName, String qName){
if(path.length()==qName.length())
path="";
else{
try{
path=path.substring(0,path.length()-((qName+tag).length()));
}catch(Exception e){}
}
}
static public void main(String[] args) {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setValidating(true);
try {
SAXParser saxParser = spf.newSAXParser();
saxParser.parse(new File(args[0]),new SAXTagCount());
} catch (Exception ex) {
System.err.println(ex.toString());
}
}
}祝你好运!
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.crimson.tree.XmlDocument;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;public void test(){XmlDocument xdoc;
String filename = "";
Document doc = null;
File docFile = new File("c:\\SystemConfig.xml");try {
//first Create DocumentBuilderFactory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//dbf.setValidating(true); //second Create DocumentBuilder
DocumentBuilder db = dbf.newDocumentBuilder(); //third Create Dom
doc = db.parse(docFile);} catch (Exception e) {
System.out.print("Problem parsing the file.");
}//STEP 1: Get the root element
Element root = doc.getDocumentElement();//STEP 2: Get the children
NodeList orders = root.getElementsByTagName("here wirte your param name");
for (int orderNum = 0; orderNum < orders.getLength(); orderNum++) { filename = orders.item(orderNum).getFirstChild().getNodeValue();
System.out.println(orders.item(orderNum).getFirstChild().getNodeValue()); }
}
没有xml文件呀!