XML文件:
<?xml version="1.0" encoding="GB2312" ?>
<books>
<book email="aaa">
<name>aName</name>
<price>aaaa</price>
</book>
<book emaiil="bbb">
<name>bName</name>
<price>bbbb</price>
</book>
</books>JAVA代码:
import java.io.FileInputStream ;
import java.io.FileNotFoundException ;
import java.io.IOException ;
import java.io.InputStream ;import javax.xml.parsers.DocumentBuilder ;
import javax.xml.parsers.DocumentBuilderFactory ;
import javax.xml.parsers.ParserConfigurationException ;import org.w3c.dom.Document ;
import org.w3c.dom.Element ;
import org.w3c.dom.Node ;
import org.w3c.dom.NamedNodeMap ;
import org.w3c.dom.NodeList ;
import org.xml.sax.SAXException ;public class ReadXML { public ReadXML(){
//(1)得到DOM解析器的工厂实例
DocumentBuilderFactory domFac = DocumentBuilderFactory.newInstance() ;
//得到javax.xml.parsers.DocumentBuilderFactory 类的实例就是我们要解析器工厂
try{
//(2)从DOM工厂获取DOM解析器
DocumentBuilder domBuilder = domFac.newDocumentBuilder() ;
//通过javax.xml.parsers.DocumentBuilderFactory实例的静态方法newDocumentBuilder()得到DOM解析器
//(3)把要解析的XML文档转化为输入流,以便DOM解析器解析
InputStream is = new FileInputStream("config.xml");//文件路径为当前class的最外层包目录
//(4)解析XML文档的输入流,得到一个Document
Document doc = domBuilder.parse(is) ;
//由XML文档的输入流得到一个org.w3c.dom.Document 对象,以后的处理都是对Document对象进行的;
//(5)得到XML文档的根节点
Element root = doc.getDocumentElement() ;
//在DOM中只有根节点是一个org.w3c.dom.Element对象
//(6)得到节点的子节点
NodeList books = root.getChildNodes() ;
if (books != null){
System.out.println("---- len :"+books.getLength() ) ;
for (int i= 0 ;i<books.getLength(); i++){
Node book = books.item(i);
if (book.getNodeType() == Node.ELEMENT_NODE){
//(7)取得节点属性值
NamedNodeMap attributes = book.getAttributes();
for (int j = 0; j < attributes.getLength(); j++) {
Node attribute = attributes.item(j);
System.out.println("book的属性名为:" + attribute.getNodeName()
+ " 相对应的属性值为:" + attribute.getNodeValue());
}
//String email = book.getAttributes().getNamedItem("email").getNodeValue() ;
//System.out.println("E-mail :"+ email) ;
//注意,节点的属性也是他的子节点,它的节点类型也是Node.ELEMENT_NODE
System.out.println("----1") ;
//(8)轮循子节点
for (Node node= book.getFirstChild(); node != null; node= node.getNextSibling()){
System.out.println("----2"+node.getNodeName()) ;
if (node.getNodeType() == Node.ELEMENT_NODE){
if (node.getNodeName().equals("name")){
String name = node.getNodeValue() ;
String name1 = node.getFirstChild().getNodeValue() ;
System.out.println("这里的name的值是空的:"+name) ;
System.out.println("xml里面的name标签值:"+name1) ;
}
if (node.getNodeName().equals("price")) {
String price = node.getFirstChild().getNodeValue() ;
System.out.println("xml里面的price标签值:"+price) ;
} }
}
}//(6)这是用一个org.w3c.dom.NodeList接口来存放它所有子节点的,还有一种轮循子节点的额犯法,后面有介绍
}
}
}catch(ParserConfigurationException e){
e.printStackTrace() ;
}catch(FileNotFoundException e){
e.printStackTrace() ;
}catch(SAXException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace() ;
}
}
public static void main(String []args){
new ReadXML() ;
}
}标红的那块代码 程序里没进去
if (node.getNodeName().equals("name")) 这句话有问题?有哪位大神,对 XML 配置文件了解的,帮帮忙吧,
在线等
<?xml version="1.0" encoding="GB2312" ?>
<books>
<book email="aaa">
<name>aName</name>
<price>aaaa</price>
</book>
<book emaiil="bbb">
<name>bName</name>
<price>bbbb</price>
</book>
</books>JAVA代码:
import java.io.FileInputStream ;
import java.io.FileNotFoundException ;
import java.io.IOException ;
import java.io.InputStream ;import javax.xml.parsers.DocumentBuilder ;
import javax.xml.parsers.DocumentBuilderFactory ;
import javax.xml.parsers.ParserConfigurationException ;import org.w3c.dom.Document ;
import org.w3c.dom.Element ;
import org.w3c.dom.Node ;
import org.w3c.dom.NamedNodeMap ;
import org.w3c.dom.NodeList ;
import org.xml.sax.SAXException ;public class ReadXML { public ReadXML(){
//(1)得到DOM解析器的工厂实例
DocumentBuilderFactory domFac = DocumentBuilderFactory.newInstance() ;
//得到javax.xml.parsers.DocumentBuilderFactory 类的实例就是我们要解析器工厂
try{
//(2)从DOM工厂获取DOM解析器
DocumentBuilder domBuilder = domFac.newDocumentBuilder() ;
//通过javax.xml.parsers.DocumentBuilderFactory实例的静态方法newDocumentBuilder()得到DOM解析器
//(3)把要解析的XML文档转化为输入流,以便DOM解析器解析
InputStream is = new FileInputStream("config.xml");//文件路径为当前class的最外层包目录
//(4)解析XML文档的输入流,得到一个Document
Document doc = domBuilder.parse(is) ;
//由XML文档的输入流得到一个org.w3c.dom.Document 对象,以后的处理都是对Document对象进行的;
//(5)得到XML文档的根节点
Element root = doc.getDocumentElement() ;
//在DOM中只有根节点是一个org.w3c.dom.Element对象
//(6)得到节点的子节点
NodeList books = root.getChildNodes() ;
if (books != null){
System.out.println("---- len :"+books.getLength() ) ;
for (int i= 0 ;i<books.getLength(); i++){
Node book = books.item(i);
if (book.getNodeType() == Node.ELEMENT_NODE){
//(7)取得节点属性值
NamedNodeMap attributes = book.getAttributes();
for (int j = 0; j < attributes.getLength(); j++) {
Node attribute = attributes.item(j);
System.out.println("book的属性名为:" + attribute.getNodeName()
+ " 相对应的属性值为:" + attribute.getNodeValue());
}
//String email = book.getAttributes().getNamedItem("email").getNodeValue() ;
//System.out.println("E-mail :"+ email) ;
//注意,节点的属性也是他的子节点,它的节点类型也是Node.ELEMENT_NODE
System.out.println("----1") ;
//(8)轮循子节点
for (Node node= book.getFirstChild(); node != null; node= node.getNextSibling()){
System.out.println("----2"+node.getNodeName()) ;
if (node.getNodeType() == Node.ELEMENT_NODE){
if (node.getNodeName().equals("name")){
String name = node.getNodeValue() ;
String name1 = node.getFirstChild().getNodeValue() ;
System.out.println("这里的name的值是空的:"+name) ;
System.out.println("xml里面的name标签值:"+name1) ;
}
if (node.getNodeName().equals("price")) {
String price = node.getFirstChild().getNodeValue() ;
System.out.println("xml里面的price标签值:"+price) ;
} }
}
}//(6)这是用一个org.w3c.dom.NodeList接口来存放它所有子节点的,还有一种轮循子节点的额犯法,后面有介绍
}
}
}catch(ParserConfigurationException e){
e.printStackTrace() ;
}catch(FileNotFoundException e){
e.printStackTrace() ;
}catch(SAXException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace() ;
}
}
public static void main(String []args){
new ReadXML() ;
}
}标红的那块代码 程序里没进去
if (node.getNodeName().equals("name")) 这句话有问题?有哪位大神,对 XML 配置文件了解的,帮帮忙吧,
在线等
解决方案 »
- poi3.7 读取excel2007 问题
- json 数据页面如何遍历
- Hibernate批量操作的事物问题,相当奇怪,大侠请进
- Spring中bulkUpdate方法 和 HibernateCallback 中 executeUpdate方法事务不起作用
- 随机密码生成问题
- tomcat5.5.20下运行struts1.1出现问题
- 一个struts比较简单的问题,对我这个初学者却感到很模糊,请帮帮忙,谢谢。
- 知道类名class,怎么找到他所在的jar?
- AXIS客户端调用过程 call.invoke()
- 进行入学注册,输入两次密码,如果相同,提示注册成功,如果不同,提示重新输入密码,java怎么写
- struts2+spring+hibernate 三大框架怎么整合
- 在一个可执行jar包里有个类a.class,同目录下还有a$1.class,a$2.class,请问这些是什么文件
本人新手,最近这个项目很烦,求帮助
本人QQ 448015593
连我这种菜鸟,都解决了啊
随便来个人,把帖子结了,要不没分郁闷了
1.先写一个接口(这一步骤可以省略)
接口代码:package hign;public interface XmlDocument
{
/**
* 建立XML文档
* @param fileName 文件全路径名称
*/
public void createXml(String fileName); /**
* 解析XML文档
* @param fileName 文件全路径名称
*/
public void parserXml(String fileName);
}
2.再写一个读取XML的类实现这个接口package hign;import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;/**
*
* Dom4j 生成XML文档与解析XML文档
*/
public class Dom4jDemo implements XmlDocument {
public static void main(String[] args)
{
Dom4jDemo d = new Dom4jDemo() ; //实例化
d.createXml("c:\\distance.xml");
d.parserXml("c:\\distance.xml");
}
public void createXml(String fileName) {
//向XML里面写入数据
Document document = DocumentHelper.createDocument();
Element stations = document.addElement("stations");
Element station = stations.addElement("station");
Element start = station.addElement("start"); //对应你的XML结构可自行修改 start end dist
start.setText("日本");
Element end = station.addElement("end");
end.setText("韩国");
Element dist = station.addElement("dist");
dist.setText("2223");
try {
Writer fileWriter = new FileWriter(fileName);
XMLWriter xmlWriter = new XMLWriter(fileWriter);
xmlWriter.write(document);
xmlWriter.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
//读入数据
public void parserXml(String fileName) //fileName可以写死掉,只读取指定的设定文件
{
File inputXml = new File(fileName);
SAXReader saxReader = new SAXReader();
saxReader.setEncoding("GBK");
try
{
Document document = saxReader.read(inputXml);
Element stations = document.getRootElement();
for(Iterator i = stations.elementIterator(); i.hasNext();)
{
Element station = (Element) i.next();
for(Iterator j = station.elementIterator(); j.hasNext();)
{
Element node =(Element) j.next();
System.out.println(node.getName() + ":" + node.getText());
}
}
}
catch (DocumentException e)
{
System.out.println(e.getMessage());
}
}
}
3.这个时候程序会有错误,但是不要紧,导入一个包就可以了
http://download.csdn.net/detail/eko208/43831134.光有代码没有XML文件也是不可以的,在此我把本人用的XML文件发出来<?xml version="1.0" encoding="UTF-8"?>
<stations>
<station>
<start>苏州</start>
<end>无锡</end>
<dist>100</dist>
</station>
</stations>
5.经过以上步骤,就可以实现JAVA代码读取 XML文件了,
但是我还有几点疑惑,有哪位朋友可以 替我解惑。
在这个类中有个写入数据的 createXml方法,如何实现多条语句的写入呢?
我尝试着这样写: Element start = station.addElement("start"); //对应你的XML结构可自行修改 start end dist
start.setText("日");
Element end = station.addElement("end");
end.setText("韩");
Element dist = station.addElement("dist");
dist.setText("2223");
Element start1 = station.addElement("start"); //对应你的XML结构可自行修改 start end dist
start.setText("中国");
Element end2 = station.addElement("end");
end.setText("韩国2");
Element dist3 = station.addElement("dist");
dist.setText("111");
但是显示结果是:
start:日2
end:韩3
dist:2223
start:
end:
dist:
也就是说 我的第二条插入语句没有成功,而且我看了下XML文件,里面也是只有start之类的标签,
但是没有实际内容的。
有哪位朋友可以告诉下,求大神!!!!