站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2EE 专题读取xml,不知道怎么读取子节点

读取xml,不知道怎么读取子节点

摘要: 读取xml,不知道怎么读取子节点


xml文件如下格式:
-
-
(车辆) 转向不足
1
,understeer
chinach,anylang

-
小小
93804
小小
chinach,anylang

-
样超杰
93805
xiaoyang
chinach,anylang




现在, 我想取得name="id" 的那个值, 并且重新写入一个新值, 不知道怎么写, 我用的是JDOM, 麻烦大侠们了!!

我的程序大致如下(屡试不爽):
String path="C://Program Files//Apache Group//Tomcat 4.1//webapps//search//webapp//WEB-INF//classes//list.xml"; //文档路径
System.out.println(path);
fi = new FileInputStream(path);
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(fi);
Element root = doc.getRootElement(); //得到根元素
List newsinfos = root.getChildren(); //得到根元素所有子元素的集合
int i = 0;
String id1 = ""; //词汇的id Element (tb_list_anylang.id)
Element newsinfo = null, newinfo1=null;
System.out.println("newsinfos.size() : "+newsinfos.size());
while(i < newsinfos.size()){
newsinfo = (Element)newsinfos.get(i);
List newinfo = newsinfo.getChildren();
String id2 = newsinfo.getChild("Field").getChild();
System.out.println(" id1 ::"+id1 + " newinfo.size() ::"+newinfo.size());
if(id1!=null && id1.trim().equals(id)){

Element anylangs = newsinfo.getChild("anylang");

String anylang = anylangs.getText();

anylang += new_anylang;

logs.info(anylang);

anylangs.setText(anylang);

XMLOutputter outp = new XMLOutputter();

fo=new FileOutputStream(path);

outp.output(doc,fo);

}
i++;
}


DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("xml文件全名");
NodeList nl = doc.getElementsByTagName("节点名");
Element node=(Element)doc.getDocumentElement();
SysPerennial.tab_shop_news_Manager = node.getElementsByTagName("属性").item(0).getFirstChild().getNodeValue();


String anylang = anylangs.getText();

getText() 是错误的 应该是 getAttribute("name");
Ok?



以下是按照你的问题我写了个例子,你可以看看:
//==================xml文件==================
<?xml version="1.0" encoding="UTF-8"?>


aaaa
1
bbbb
cccc


dddd
93804
eeee
ffff


//===========================================

//=================测试类====================
package ceng;

import javax.xml.parsers.*;
import org.w3c.dom.Document;
import java.io.IOException;
import org.xml.sax.SAXException;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import java.io.File;


public class aaaa {
public static void main(String[] args) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document doc = null;
try {
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse("F:/DataFactoryName.xml");
}
catch (IOException ex) {
System.out.println("=====>"+ex);
ex.printStackTrace();
}
catch (SAXException ex) {
System.out.println("=====>"+ex);
ex.printStackTrace();
}
catch (ParserConfigurationException ex) {
System.out.println("=====>"+ex);
ex.printStackTrace();
}
NodeList nl = doc.getElementsByTagName("newsinfo");
Element node=(Element)doc.getDocumentElement();
String str1 = node.getElementsByTagName("Field").item(0).getFirstChild().getNodeValue();
String str2 = node.getElementsByTagName("Field").item(1).getFirstChild().getNodeValue();
String str3 = node.getElementsByTagName("Field").item(2).getFirstChild().getNodeValue();
String str4 = node.getElementsByTagName("Index").item(0).getFirstChild().getNodeValue();
System.out.println("========>"+str1);
System.out.println("========>"+str2);
System.out.println("========>"+str3);
System.out.println("========>"+str4);
}
}
//============================================

//==================输出结果==================
========>aaaa

========>1

========>bbbb

========>cccc

//============================================



就你这到只取ID的问题你可以这样写:
public class aaaa {
public static void main(String[] args) throws ParserConfigurationException,
IOException, SAXException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document doc = null;
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse("F:/DataFactoryName.xml");
NodeList nl = doc.getElementsByTagName("newsinfo");
Element node = (Element) doc.getDocumentElement();
for(int i=1;i String str = node.getElementsByTagName("Field").item(i).getFirstChild().getNodeValue();
System.out.println("========>" + str);
}
}
}
当然我的这个方法不一定是最好的,工大家讨论以下.


无情接分,友情up!


应该没有什么问题吧


name那里是属性,不是value。
楼主get错了。


↑返回目录
前一篇: 关于Timer中调用全局静态变量的疑惑
后一篇: ★★★★★★大家来看看啊,弄一天了,问题还是没解决★★★★★★