各位大哥,大姐帮帮忙,xml解析问题,用DOM解析,解决就给分,分不够再加分 不要用dom,用jdom,在网上搜艘,一堆一堆的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 google search java xml解析好多代码的啊 在java对XML进行处理时,读取XML文档,对其处理,这是我得一个实例代码。import java.io.FileInputStream;import javax.xml.parsers.*;import org.w3c.dom.*;/* * Created on 2004-6-2 * *java读取XML文档 *利用DoM来读取一个XML文档的内容,并将其打印出来 * *//** * @author wzy * * Email:[email protected] */public class TestXML { public static void main(String[] args) { Document doc; DocumentBuilderFactory factory; DocumentBuilder docBuilder; Element root; String elementName; FileInputStream in; String fileName; try{ //get the xml file fileName = System.getProperty("user.dir"); fileName = fileName+"/sample.xml"; in = new FileInputStream(fileName); //解析XML文件,生成document对象 factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); docBuilder = factory.newDocumentBuilder(); doc = docBuilder.parse(in); //解析成功 System.out.println("parse successfull"); //获取XML文档的根节点 root = doc.getDocumentElement(); elementName = root.getNodeName(); //打印根节点的属性 printAttributes(root); //打印该文档全部节点 System.out.println("打印全部节点"); printElement(root,0); } catch(Exception exp){ exp.printStackTrace(); } } //打印某个节点的全部属性 public static void printAttributes(Element elem){ NamedNodeMap attributes; int i,max; String name,value; Node curNode; attributes = elem.getAttributes(); max = attributes.getLength(); for(i=0;i<max;i++){ curNode = attributes.item(i); name = curNode.getNodeName(); value = curNode.getNodeValue(); System.out.println("\t"+name+" = "+value); } } //打印所有的节点的名称和值 //改方法采用递归方式打印文档的全部节点 public static void printElement(Element elem,int depth){ String elementName; NodeList children; int i,max; Node curChild; Element curElement; String nodeName,nodeValue; //elementName = elem.getNodeName(); //获取输入节点的全部子节点 children = elem.getChildNodes(); //按一定格式打印输入节点 for(int j=0;j<depth;j++){ System.out.print(" "); } printAttributes(elem); //采用递归方式打印全部子节点 max = children.getLength(); for(i=0;i<max;i++){ curChild = children.item(i); //递归退出条件 if(curChild instanceof Element){ curElement = (Element)curChild; printElement(curElement,depth+1); } else{ nodeName = curChild.getNodeName(); nodeValue = curChild.getNodeValue(); for(int j=0;j<depth;j++)System.out.print(" "); System.out.println(nodeName+" = "+nodeValue); } } }}---------------------------------------------------------------------以上是别人的文章,相信你的问题非常类似 import java.sql.*;import javax.xml.parsers.*;import org.apache.xpath.*;import org.apache.crimson.tree.*;import javax.xml.transform.dom.*;import org.w3c.dom.*;import java.util.*;import javax.xml.transform.stream.*;import javax.xml.transform.*;import java.io.*;import java.util.*;public class BuildDeptXML{ Connection conn=null; String sql; int i=0; ArrayList list=new ArrayList(); File f=new File("dept.xml"); Element totalname=null; Element department=null; Element role=null; Element person=null; Element know=null; String deptID=""; String roleID=""; boolean bool=false; boolean bool1=false; boolean bool2=false; public void BuildXml() { try { if(!f.exists()) { f.createNewFile(); } DocumentBuilderFactory fact=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=fact.newDocumentBuilder(); Document doc=builder.newDocument(); conn=DBConnection.getConnection(); sql="select * from dept"; Statement stm=conn.createStatement(); ResultSet rs=stm.executeQuery(sql); totalname=doc.createElement("TOTALNAME"); totalname.setAttribute("TOTALNAME","Сǿ"); while(rs.next()) { deptID=rs.getString(1); department=doc.createElement("DEPARTMENT"); totalname.appendChild(department); department.setAttribute("DEPTID",rs.getString(1)); department.setAttribute("DEPTNAME",rs.getString(3));while(result.next())// { sql="select * from role"; conn=DBConnection.getConnection(); stm=conn.createStatement(); ResultSet rule=stm.executeQuery(sql); while(rule.next()) { sql="select * from teacher where roleid='"+rule.getString(1)+"'and deptid='"+deptID+"'"; conn=DBConnection.getConnection(); stm=conn.createStatement(); ResultSet teach=stm.executeQuery(sql); if(teach.next()) { role=doc.createElement("ROLE"); department.appendChild(role); role.setAttribute("ROLEID",rule.getString(1)); role.setAttribute("ROLENAME",rule.getString(2)); sql="select * from teacher where roleid='"+rule.getString(1)+"'and deptid='"+deptID+"'"; conn=DBConnection.getConnection(); stm=conn.createStatement(); ResultSet teacher=stm.executeQuery(sql); while(teacher.next()) { person=doc.createElement("PERSON"); role.appendChild(person); person.setAttribute("PERSONID",teacher.getString(1)); person.setAttribute("PERSONNAME",teacher.getString(4)); } } } } 求前辈解答 struts2的问题 初级jsp程序员的小小愿望 struts2中请求返回到jsp页面提示下载 本人在做一个的“参数化的视图”,现在有些问题,请大家一起讨论一下,给我点意见 请问怎样在删除按钮中得到要删除的表的ID 大虾帮帮忙~拜托 struts配置action 调试时总是提示找不到该**.do 怎么通过代码取得当前classpath下某一包中的所有类(急) 用spring做的web应用部署到tomcat5后出错 关于jdbc连接池 junit方法的测试须序问题
好多代码的
啊
* Created on 2004-6-2
*
*java读取XML文档
*利用DoM来读取一个XML文档的内容,并将其打印出来
*
*//**
* @author wzy
*
* Email:[email protected]
*/
public class TestXML { public static void main(String[] args) {
Document doc;
DocumentBuilderFactory factory;
DocumentBuilder docBuilder;
Element root;
String elementName;
FileInputStream in;
String fileName;
try{
//get the xml file
fileName = System.getProperty("user.dir");
fileName = fileName+"/sample.xml";
in = new FileInputStream(fileName);
//解析XML文件,生成document对象
factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
docBuilder = factory.newDocumentBuilder();
doc = docBuilder.parse(in);
//解析成功
System.out.println("parse successfull");
//获取XML文档的根节点
root = doc.getDocumentElement();
elementName = root.getNodeName();
//打印根节点的属性
printAttributes(root);
//打印该文档全部节点
System.out.println("打印全部节点");
printElement(root,0);
}
catch(Exception exp){
exp.printStackTrace();
}
}
//打印某个节点的全部属性
public static void printAttributes(Element elem){
NamedNodeMap attributes;
int i,max;
String name,value;
Node curNode;
attributes = elem.getAttributes();
max = attributes.getLength();
for(i=0;i<max;i++){
curNode = attributes.item(i);
name = curNode.getNodeName();
value = curNode.getNodeValue();
System.out.println("\t"+name+" = "+value);
}
} //打印所有的节点的名称和值
//改方法采用递归方式打印文档的全部节点
public static void printElement(Element elem,int depth){
String elementName;
NodeList children;
int i,max;
Node curChild;
Element curElement;
String nodeName,nodeValue;
//elementName = elem.getNodeName();
//获取输入节点的全部子节点
children = elem.getChildNodes();
//按一定格式打印输入节点
for(int j=0;j<depth;j++){
System.out.print(" ");
}
printAttributes(elem);
//采用递归方式打印全部子节点
max = children.getLength();
for(i=0;i<max;i++){
curChild = children.item(i);
//递归退出条件
if(curChild instanceof Element){
curElement = (Element)curChild;
printElement(curElement,depth+1);
}
else{
nodeName = curChild.getNodeName();
nodeValue = curChild.getNodeValue();
for(int j=0;j<depth;j++)System.out.print(" ");
System.out.println(nodeName+" = "+nodeValue);
}
}
}
}
---------------------------------------------------------------------
以上是别人的文章,相信你的问题非常类似
import javax.xml.parsers.*;
import org.apache.xpath.*;
import org.apache.crimson.tree.*;
import javax.xml.transform.dom.*;
import org.w3c.dom.*;
import java.util.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.*;
import java.io.*;
import java.util.*;
public class BuildDeptXML
{
Connection conn=null;
String sql;
int i=0;
ArrayList list=new ArrayList();
File f=new File("dept.xml");
Element totalname=null;
Element department=null;
Element role=null;
Element person=null;
Element know=null;
String deptID="";
String roleID="";
boolean bool=false;
boolean bool1=false;
boolean bool2=false;
public void BuildXml()
{
try
{
if(!f.exists())
{
f.createNewFile();
}
DocumentBuilderFactory fact=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=fact.newDocumentBuilder();
Document doc=builder.newDocument();
conn=DBConnection.getConnection();
sql="select * from dept";
Statement stm=conn.createStatement();
ResultSet rs=stm.executeQuery(sql);
totalname=doc.createElement("TOTALNAME");
totalname.setAttribute("TOTALNAME","Сǿ");
while(rs.next())
{
deptID=rs.getString(1);
department=doc.createElement("DEPARTMENT");
totalname.appendChild(department);
department.setAttribute("DEPTID",rs.getString(1));
department.setAttribute("DEPTNAME",rs.getString(3));
while(result.next())
// {
sql="select * from role"; conn=DBConnection.getConnection();
stm=conn.createStatement();
ResultSet rule=stm.executeQuery(sql);
while(rule.next())
{
sql="select * from teacher where roleid='"+rule.getString(1)+"'and deptid='"+deptID+"'";
conn=DBConnection.getConnection();
stm=conn.createStatement();
ResultSet teach=stm.executeQuery(sql);
if(teach.next())
{
role=doc.createElement("ROLE");
department.appendChild(role);
role.setAttribute("ROLEID",rule.getString(1));
role.setAttribute("ROLENAME",rule.getString(2));
sql="select * from teacher where roleid='"+rule.getString(1)+"'and deptid='"+deptID+"'";
conn=DBConnection.getConnection();
stm=conn.createStatement();
ResultSet teacher=stm.executeQuery(sql);
while(teacher.next())
{
person=doc.createElement("PERSON");
role.appendChild(person);
person.setAttribute("PERSONID",teacher.getString(1));
person.setAttribute("PERSONNAME",teacher.getString(4));
}
}
}
}