求java读写XML的例子。谢谢。给分100。 还要java读写xml数据库的。再次谢过。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://dev.csdn.net/article/20/20721.shtm 给你两个地址,自己去看吧DOM4J 使用简介 http://icecloud.51.net/blog/archives/000009.html 使用 dom4j 解析 XML http://www-900.ibm.com/developerWorks/cn/xml/x-dom4j.shtml http://blog.csdn.net/mail25/archive/2004/07/07/36040.aspxSAX方法,读取的速度比较快啊!DOM的也有,就是找不到放哪了!郁闷 下面这是个写XML的,不过是基于GUI的,道理是一样嘛!===============================================要导入import javax.xml.parsers.*;import org.w3c.dom.Document;import org.w3c.dom.Element;才行 private void saveData(){ String DES ; try{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.newDocument(); // Create DOM Tree Element root = document.createElement("AimsLicense"); document.appendChild(root); Element Company = document.createElement("Company"); Element ComInfo = document.createElement("ComInfo"); Element UseInfo = document.createElement("UseInfo"); Element OthInfo = document.createElement("OthInfo"); root.appendChild(Company); Element NameLong = document.createElement("NameLong"); Element NameShort = document.createElement("NameShort"); Element Location = document.createElement("Location"); Element Address = document.createElement("Address"); Element TermDate = document.createElement("TermDate"); Element Contact = document.createElement("Contact"); Element ContactMail = document.createElement("ContactMail"); Element IssueDate = document.createElement("IssueDate"); Element UserNum = document.createElement("UserNum"); Element PassDue = document.createElement("PassDue"); Element LicEnc = document.createElement("LicEnc"); Element SoftwareName = document.createElement("SoftwareName"); Element ComponentName = document.createElement("ComponentName"); Element SoftekMail = document.createElement("SoftekMail"); Company.appendChild(ComInfo); Company.appendChild(UseInfo); Company.appendChild(OthInfo); ComInfo.appendChild(NameLong); ComInfo.appendChild(NameShort); ComInfo.appendChild(Location); ComInfo.appendChild(Address); UseInfo.appendChild(Contact); UseInfo.appendChild(ContactMail); UseInfo.appendChild(TermDate); UseInfo.appendChild(IssueDate); UseInfo.appendChild(UserNum); UseInfo.appendChild(PassDue); OthInfo.appendChild(SoftwareName); OthInfo.appendChild(ComponentName); OthInfo.appendChild(SoftekMail); OthInfo.appendChild(LicEnc); DES = LicenseAdmin.getInstance().LicenseEnc(txtLong.getText()+txtTerm.getText()+txtUser.getText()+txtPass.getText()+txtSoftek.getText()+txtSoft.getText()+txtCompo.getText()); NameLong.appendChild(document.createTextNode(txtLong.getText())); NameShort.appendChild(document.createTextNode(txtShort.getText())); Location.appendChild(document.createTextNode(txtLocation.getText())); Address.appendChild(document.createTextNode(txtAddress.getText())); Contact.appendChild(document.createTextNode(txtContact.getText())); ContactMail.appendChild(document.createTextNode(txtContactMail.getText())); TermDate.appendChild(document.createTextNode(txtTerm.getText())); IssueDate.appendChild(document.createTextNode(txtIssue.getText())); UserNum.appendChild(document.createTextNode(txtUser.getText())); PassDue.appendChild(document.createTextNode(txtPass.getText())); SoftwareName.appendChild(document.createTextNode(txtSoft.getText())); ComponentName.appendChild(document.createTextNode(txtCompo.getText())); SoftekMail.appendChild(document.createTextNode(txtSoftek.getText())); LicEnc.appendChild(document.createCDATASection(DES)); if(LicenseAdmin.getInstance().generateLicXML(document)){ JOptionPane.showConfirmDialog(this, "生成License文件成功,请查看!", "提示信息", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE); }else{ JOptionPane.showConfirmDialog(this, "生成License文件失败,请查看!", "错误信息", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE); } }catch(Exception e){ e.printStackTrace(); } }====================================读的方法在这 private DocumentBuilderFactory factory; private DocumentBuilder builder; private Document doc; private int Days; private static final String BASEDIR="app.basedir";//web.xml中指定了初始化目录的地址,也可以用String指定,不用Properties属性 private static final String SEPARATOR="file.separator";//不同操作系统中的分隔符 private String file_uri = ""; Properties properties = System.getProperties(); String separator = properties.getProperty(SEPARATOR); file_uri = softeksystem.com.PropertyMgr.getInstance().getProperty(BASEDIR); file_uri = file_uri.concat("AimsLicense.xml"); try { factory = DocumentBuilderFactory.newInstance(); builder = factory.newDocumentBuilder(); doc = builder.parse(file_uri);//这个doc可以取代上面新产生的那个,做为读入的,然后可以进行相应的修改,上面是的GUI用的!有点乱,不知道能看明白了! }catch(Ex package xml;import javax.xml.parsers.*;import org.w3c.dom.*;import java.io.*;//import java.lang.String; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.FactoryConfigurationError; import javax.xml.parsers.ParserConfigurationException; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import java.io.File; import java.io.IOException; import org.w3c.dom.*;public class Xml{ private Element e; Node[] tagNode; Node[] proNode; private String[] tagNameList; private String[] proNameList; //set xml's object from xml's name public void xmlObj(String name) { try{ DocumentBuilderFactory dof=DocumentBuilderFactory.newInstance(); DocumentBuilder db=dof.newDocumentBuilder(); Document document = db.parse(new File(name)); e = document.getDocumentElement(); } catch(Exception e){ System.out.println("file's Exception"); } } //get xml's object public Element getElement(){ System.out.println(e); return e; } /** //set tag's name by tagNode public void setTag(String eleName){ Element el = getElement(); int length = el.getElementsByTagName(eleName).getLength(); tagNode = new Node[length]; int loop; for(loop = 0;loop < length;loop++){ tagNode[loop] = el.getElementsByTagName(eleName).item(loop); System.out.println(tagNode[loop]); } } //get tag's name from tagNode public Node[] getTag(){ return tagNode; }**/ //set tag's name by tagNameList public void setTagName(String name) { try{ //this.name = name; //xml.xmlObj("Tag.xml"); int length = e.getElementsByTagName("tag").getLength(); tagNameList = new String[length]; for(int loop = 0;loop < length;loop++){ tagNameList[loop] = e.getElementsByTagName("tag").item(loop).getAttributes().getNamedItem(name).getNodeValue(); System.out.println(tagNameList[loop]); } //for(int step = 0;step<nameList.length;step++) //System.out.println(nameList[step]); } catch(Exception e){ System.out.println(e); } } //get tag's name from tagNameList public String[] getTagName(){ // System.out.println(nameList[0]); return tagNameList; }// public void setPro(String tagName,String proName){// setTag(tagName);// tagNode = getTag(); //tagNode[0].getChildNodes();// System.out.println(tagNode[2].getAttributes());//getChildNodes().item(2).getOwnerDocument().getElementsByTagName("property").item(6).getNodeName());//.getNodeType());// } //public Node getPro(){ //} //set property's name by proName public void setPro(String proName){ try{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse( new File("g:/hyper editor/Tag.xml") ); NodeList listProperty = document.getElementsByTagName("tag"); for (int i=0; i< listProperty.getLength(); i++){ Node node = listProperty.item(i); NamedNodeMap attrs = node.getAttributes(); Node attrNode = attrs.item(0); String attrValue = attrNode.getNodeValue(); /////////////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!/////////// if(attrValue.equals(proName)){ NodeList childs = node.getChildNodes() ; int a = 0; for (int j=0; j< childs.getLength(); j++){ Node current = childs.item(j); if(current.getNodeType() == current.ELEMENT_NODE){ NamedNodeMap propertyAttrs = current.getAttributes(); Node proprtyattrNode = propertyAttrs.item(0); //String proList = proprtyattrNode.getNodeValue(); String[] proList = new String[100]; for(int loop = 0;loop < proList.length; loop ++) proList[loop] = proprtyattrNode.getNodeValue(); //proListName[0] = proList; // System.out.println(proList); //System.out.print(proprtyattrNode.getNodeValue() +" = "); Node fisrtchild = current.getFirstChild(); if(fisrtchild != null){ String value = fisrtchild.getNodeValue(); //System.out.println(value); } // else // System.out.println("no value exists"); if(proList != null){ System.out.println(proList[4]); //proListName[loop++] = proList; //System.out.println(proListName[loop++]); } } } } } } catch (SAXParseException spe) { // Error generated by the parser System.out.println("\n** Parsing error" + ", line " + spe.getLineNumber() + ", uri " + spe.getSystemId()); System.out.println(" " + spe.getMessage() ); // Use the contained exception, if any Exception x = spe; if (spe.getException() != null) x = spe.getException(); x.printStackTrace(); } catch (SAXException sxe) { // Error generated during parsing Exception x = sxe; if (sxe.getException() != null) x = sxe.getException(); x.printStackTrace(); } catch (ParserConfigurationException pce) { // Parser with specified options can't be built pce.printStackTrace(); } catch (IOException ioe) { // I/O error ioe.printStackTrace(); } } //get property's name from tag's name public String getPro(){ // System.out.println(proListName[1]); // return proListName; return "hehehehhehehe"; } public static void main(String args[]) throws Exception{ Xml xml = new Xml(); xml.xmlObj("g:/hyper editor/Tag.xml"); //xml.getElement(); xml.setTagName("name"); xml.getTagName(); //xml.setPro("tag","name"); xml.setPro("Table"); }/** thinking static Document document; public static void main(String argv[]) { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.parse( new File("tag.xml") ); NodeList listProperty = document.getElementsByTagName("tag"); for (int i=0; i< listProperty.getLength(); i++) { Node node = listProperty.item(i); NamedNodeMap attrs = node.getAttributes(); Node attrNode = attrs.item(0); String attrValue = attrNode.getNodeValue(); if(attrValue.equals("Table")) { NodeList childs = node.getChildNodes() ; for (int j=0; j< childs.getLength(); j++) { Node current = childs.item(j); if(current.getNodeType() == current.ELEMENT_NODE) { NamedNodeMap propertyAttrs = current.getAttributes(); Node proprtyattrNode = propertyAttrs.item(0); String proList = proprtyattrNode.getNodeValue(); System.out.println(proList); //System.out.print(proprtyattrNode.getNodeValue() +" = "); Node fisrtchild = current.getFirstChild(); if(fisrtchild != null){ String value = fisrtchild.getNodeValue(); //System.out.println(value); } else System.out.println("no value exists"); } } } } } catch (SAXParseException spe) { // Error generated by the parser System.out.println("\n** Parsing error" + ", line " + spe.getLineNumber() + ", uri " + spe.getSystemId()); System.out.println(" " + spe.getMessage() ); // Use the contained exception, if any Exception x = spe; if (spe.getException() != null) x = spe.getException(); x.printStackTrace(); } catch (SAXException sxe) { // Error generated during parsing Exception x = sxe; if (sxe.getException() != null) x = sxe.getException(); x.printStackTrace(); } catch (ParserConfigurationException pce) { // Parser with specified options can't be built pce.printStackTrace(); } catch (IOException ioe) { // I/O error ioe.printStackTrace(); } }// main/***************************************/ /** public static void main(String args[]) throws Exception{ DocumentBuilderFactory dof=DocumentBuilderFactory.newInstance(); DocumentBuilder db=dof.newDocumentBuilder(); Document d=db.parse(new File("Tag.xml")); Element e=d.getDocumentElement();//取根元素webapp System.out.println(e); // NodeList nl=e.getElementsByTagName("urlName");//取urlName元素列表 NodeList nl=e.getElementsByTagName("tag"); System.out.println(nl.item(0).getAttributes().getNamedItem("name").getNodeValue()); NodeList nlChild = e.getElementsByTagName("pro"); System.out.println(nlChild.item(0).getAttributes().getNamedItem("name").getNodeValue()); //nl.item(0)取的列表中第一\u9879 // System.out.println(nl); } */}里面有你要的方法,呵呵,自己慢慢整理一下把 疯了,狂贴代码!去下载个Digester (apache-jakarta)http://www.javayou.com/showlog.jspe?cat_id=-1&log_id=154http://www.javayou.com (Java自由人) 内部类怎么办 jdbc 数据库登录时出现问题了!! 关于java参数的问题 ResultSet的getObject方法为什么不能提取sysname类型字段的值 war包 applet 求助 我发现了csdn中的一个bug applet 的半透明效果 [求助]Java 安全套接字编程中SSLContext如果不指定keymanager也不指定trustmanager,意义何在? 问一个paintComponent的问题 天下第一傻瓜问题,大家莫笑话啊! 谁可以给我解释一下“import java.awt.event.*’和在TOOL\CONFIGN LIBRAY的区别?
使用 dom4j 解析 XML http://www-900.ibm.com/developerWorks/cn/xml/x-dom4j.shtml
SAX方法,读取的速度比较快啊!
DOM的也有,就是找不到放哪了!郁闷
===============================================
要导入
import javax.xml.parsers.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
才行
private void saveData(){
String DES ;
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
// Create DOM Tree
Element root = document.createElement("AimsLicense");
document.appendChild(root);
Element Company = document.createElement("Company");
Element ComInfo = document.createElement("ComInfo");
Element UseInfo = document.createElement("UseInfo");
Element OthInfo = document.createElement("OthInfo");
root.appendChild(Company);
Element NameLong = document.createElement("NameLong");
Element NameShort = document.createElement("NameShort");
Element Location = document.createElement("Location");
Element Address = document.createElement("Address");
Element TermDate = document.createElement("TermDate");
Element Contact = document.createElement("Contact");
Element ContactMail = document.createElement("ContactMail");
Element IssueDate = document.createElement("IssueDate");
Element UserNum = document.createElement("UserNum");
Element PassDue = document.createElement("PassDue");
Element LicEnc = document.createElement("LicEnc");
Element SoftwareName = document.createElement("SoftwareName");
Element ComponentName = document.createElement("ComponentName");
Element SoftekMail = document.createElement("SoftekMail");
Company.appendChild(ComInfo);
Company.appendChild(UseInfo);
Company.appendChild(OthInfo); ComInfo.appendChild(NameLong);
ComInfo.appendChild(NameShort);
ComInfo.appendChild(Location);
ComInfo.appendChild(Address); UseInfo.appendChild(Contact);
UseInfo.appendChild(ContactMail);
UseInfo.appendChild(TermDate);
UseInfo.appendChild(IssueDate);
UseInfo.appendChild(UserNum);
UseInfo.appendChild(PassDue); OthInfo.appendChild(SoftwareName);
OthInfo.appendChild(ComponentName);
OthInfo.appendChild(SoftekMail);
OthInfo.appendChild(LicEnc); DES = LicenseAdmin.getInstance().LicenseEnc(txtLong.getText()+txtTerm.getText()+txtUser.getText()+txtPass.getText()+txtSoftek.getText()+txtSoft.getText()+txtCompo.getText()); NameLong.appendChild(document.createTextNode(txtLong.getText()));
NameShort.appendChild(document.createTextNode(txtShort.getText()));
Location.appendChild(document.createTextNode(txtLocation.getText()));
Address.appendChild(document.createTextNode(txtAddress.getText()));
Contact.appendChild(document.createTextNode(txtContact.getText()));
ContactMail.appendChild(document.createTextNode(txtContactMail.getText()));
TermDate.appendChild(document.createTextNode(txtTerm.getText()));
IssueDate.appendChild(document.createTextNode(txtIssue.getText()));
UserNum.appendChild(document.createTextNode(txtUser.getText()));
PassDue.appendChild(document.createTextNode(txtPass.getText()));
SoftwareName.appendChild(document.createTextNode(txtSoft.getText()));
ComponentName.appendChild(document.createTextNode(txtCompo.getText()));
SoftekMail.appendChild(document.createTextNode(txtSoftek.getText())); LicEnc.appendChild(document.createCDATASection(DES)); if(LicenseAdmin.getInstance().generateLicXML(document)){
JOptionPane.showConfirmDialog(this, "生成License文件成功,请查看!", "提示信息",
JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE);
}else{
JOptionPane.showConfirmDialog(this, "生成License文件失败,请查看!", "错误信息",
JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE);
} }catch(Exception e){ e.printStackTrace(); } }====================================
读的方法在这
private DocumentBuilderFactory factory;
private DocumentBuilder builder;
private Document doc;
private int Days; private static final String BASEDIR="app.basedir";//web.xml中指定了初始化目录的地址,也可以用String指定,不用Properties属性
private static final String SEPARATOR="file.separator";//不同操作系统中的分隔符
private String file_uri = ""; Properties properties = System.getProperties();
String separator = properties.getProperty(SEPARATOR);
file_uri = softeksystem.com.PropertyMgr.getInstance().getProperty(BASEDIR);
file_uri = file_uri.concat("AimsLicense.xml");
try {
factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
doc = builder.parse(file_uri);//这个doc可以取代上面新产生的那个,做为读入的,然后可以进行相应的修改,上面是的GUI用的!
有点乱,不知道能看明白了!
}catch(Ex
import org.w3c.dom.*;
import java.io.*;
//import java.lang.String;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import java.io.File;
import java.io.IOException; import org.w3c.dom.*;
public class Xml{
private Element e;
Node[] tagNode;
Node[] proNode;
private String[] tagNameList;
private String[] proNameList;
//set xml's object from xml's name
public void xmlObj(String name) {
try{
DocumentBuilderFactory dof=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dof.newDocumentBuilder();
Document document = db.parse(new File(name));
e = document.getDocumentElement();
}
catch(Exception e){
System.out.println("file's Exception");
} } //get xml's object
public Element getElement(){
System.out.println(e);
return e;
}
/**
//set tag's name by tagNode
public void setTag(String eleName){
Element el = getElement();
int length = el.getElementsByTagName(eleName).getLength();
tagNode = new Node[length];
int loop;
for(loop = 0;loop < length;loop++){
tagNode[loop] = el.getElementsByTagName(eleName).item(loop);
System.out.println(tagNode[loop]);
}
} //get tag's name from tagNode
public Node[] getTag(){ return tagNode; }
**/ //set tag's name by tagNameList
public void setTagName(String name) {
try{
//this.name = name;
//xml.xmlObj("Tag.xml");
int length = e.getElementsByTagName("tag").getLength();
tagNameList = new String[length];
for(int loop = 0;loop < length;loop++){
tagNameList[loop] = e.getElementsByTagName("tag").item(loop).getAttributes().getNamedItem(name).getNodeValue();
System.out.println(tagNameList[loop]);
}
//for(int step = 0;step<nameList.length;step++)
//System.out.println(nameList[step]);
}
catch(Exception e){
System.out.println(e);
}
} //get tag's name from tagNameList
public String[] getTagName(){
// System.out.println(nameList[0]);
return tagNameList; }// public void setPro(String tagName,String proName){
// setTag(tagName);
// tagNode = getTag();
//tagNode[0].getChildNodes();
// System.out.println(tagNode[2].getAttributes());//getChildNodes().item(2).getOwnerDocument().getElementsByTagName("property").item(6).getNodeName());//.getNodeType());
// }
//public Node getPro(){ //} //set property's name by proName
public void setPro(String proName){
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse( new File("g:/hyper editor/Tag.xml") );
NodeList listProperty = document.getElementsByTagName("tag");
for (int i=0; i< listProperty.getLength(); i++){
Node node = listProperty.item(i);
NamedNodeMap attrs = node.getAttributes();
Node attrNode = attrs.item(0);
String attrValue = attrNode.getNodeValue();
/////////////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!///////////
if(attrValue.equals(proName)){
NodeList childs = node.getChildNodes() ;
int a = 0;
for (int j=0; j< childs.getLength(); j++){
Node current = childs.item(j);
if(current.getNodeType() == current.ELEMENT_NODE){
NamedNodeMap propertyAttrs = current.getAttributes();
Node proprtyattrNode = propertyAttrs.item(0);
//String proList = proprtyattrNode.getNodeValue();
String[] proList = new String[100];
for(int loop = 0;loop < proList.length; loop ++)
proList[loop] = proprtyattrNode.getNodeValue();
//proListName[0] = proList;
// System.out.println(proList);
//System.out.print(proprtyattrNode.getNodeValue() +" = "); Node fisrtchild = current.getFirstChild();
if(fisrtchild != null){
String value = fisrtchild.getNodeValue();
//System.out.println(value);
}
// else
// System.out.println("no value exists");
if(proList != null){ System.out.println(proList[4]);
//proListName[loop++] = proList;
//System.out.println(proListName[loop++]);
}
}
}
}
} }
catch (SAXParseException spe) { // Error generated by the parser
System.out.println("\n** Parsing error"
+ ", line " + spe.getLineNumber()
+ ", uri " + spe.getSystemId());
System.out.println(" " + spe.getMessage() ); // Use the contained exception, if any
Exception x = spe;
if (spe.getException() != null)
x = spe.getException();
x.printStackTrace(); } catch (SAXException sxe) { // Error generated during parsing
Exception x = sxe;
if (sxe.getException() != null)
x = sxe.getException();
x.printStackTrace(); } catch (ParserConfigurationException pce) { // Parser with specified options can't be built
pce.printStackTrace(); } catch (IOException ioe) { // I/O error
ioe.printStackTrace();
}
} //get property's name from tag's name
public String getPro(){
// System.out.println(proListName[1]);
// return proListName;
return "hehehehhehehe";
}
public static void main(String args[]) throws Exception{
Xml xml = new Xml();
xml.xmlObj("g:/hyper editor/Tag.xml");
//xml.getElement();
xml.setTagName("name");
xml.getTagName();
//xml.setPro("tag","name");
xml.setPro("Table");
}/** thinking
static Document document;
{
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse( new File("tag.xml") ); NodeList listProperty = document.getElementsByTagName("tag");
for (int i=0; i< listProperty.getLength(); i++)
{ Node node = listProperty.item(i);
NamedNodeMap attrs = node.getAttributes();
Node attrNode = attrs.item(0);
String attrValue = attrNode.getNodeValue();
if(attrValue.equals("Table"))
{
NodeList childs = node.getChildNodes() ;
for (int j=0; j< childs.getLength(); j++)
{ Node current = childs.item(j);
if(current.getNodeType() == current.ELEMENT_NODE)
{
NamedNodeMap propertyAttrs = current.getAttributes();
Node proprtyattrNode = propertyAttrs.item(0);
String proList = proprtyattrNode.getNodeValue();
System.out.println(proList);
//System.out.print(proprtyattrNode.getNodeValue() +" = "); Node fisrtchild = current.getFirstChild();
if(fisrtchild != null){
String value = fisrtchild.getNodeValue();
//System.out.println(value);
}
else
System.out.println("no value exists");
} } } } } catch (SAXParseException spe) { // Error generated by the parser
System.out.println("\n** Parsing error"
+ ", line " + spe.getLineNumber()
+ ", uri " + spe.getSystemId());
System.out.println(" " + spe.getMessage() ); // Use the contained exception, if any
Exception x = spe;
if (spe.getException() != null)
x = spe.getException();
x.printStackTrace(); } catch (SAXException sxe) { // Error generated during parsing
Exception x = sxe;
if (sxe.getException() != null)
x = sxe.getException();
x.printStackTrace(); } catch (ParserConfigurationException pce) { // Parser with specified options can't be built
pce.printStackTrace(); } catch (IOException ioe) { // I/O error
ioe.printStackTrace();
} }// main
/***************************************/
/**
public static void main(String args[]) throws Exception{
DocumentBuilderFactory dof=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dof.newDocumentBuilder();
Document d=db.parse(new File("Tag.xml"));
Element e=d.getDocumentElement();//取根元素webapp
System.out.println(e);
// NodeList nl=e.getElementsByTagName("urlName");//取urlName元素列表
NodeList nl=e.getElementsByTagName("tag");
System.out.println(nl.item(0).getAttributes().getNamedItem("name").getNodeValue());
NodeList nlChild = e.getElementsByTagName("pro");
System.out.println(nlChild.item(0).getAttributes().getNamedItem("name").getNodeValue()); //nl.item(0)取的列表中第一\u9879
// System.out.println(nl); }
*/
}
里面有你要的方法,呵呵,自己慢慢整理一下把