关于DOM中获得某结点的第N个子结点的结点名称问题

hellolwl 2003-08-22 11:12:29
我在java中用DOM解析XML文档后,想得到某结点的第N个子结点的结点名称,应该怎么用?以前我都是通过getElementsByTagName来获得某结点的子结点的,如果我不知道结点名称应该怎么获得?不吝赐教,谢谢!
...全文
141 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dickensi 2003-08-23
  • 打赏
  • 举报
回复
ps: 有许多dom实现可以让我们更容易地使用dom
比如dom4j,jdom...可以提高效率

要运行上例子你把Outer.pl替换成system.Out.println
Dickensi 2003-08-23
  • 打赏
  • 举报
回复
不晓得你那个包支不支持xpath:

org.w3c.dom.Document 把任何东西都定义为node, attribute, element, text 都属于一个node, 所以你那里返回#text是正常的,propetyNode.getChildNodes().item(0)返回的其实是
CGeoMLFeatureProperty 文本,你要获取他的tagname 当然是#text:你可以试试以下代码:

static public void testW3CDom() throws Exception{
org.w3c.dom.Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
ByteArrayInputStream bis = new ByteArrayInputStream("<tst> <element1>2323</element1><dickensi>myvalue</dickensi></tst>".getBytes());
doc = db.parse(bis) ;

NodeList list = doc.getDocumentElement().getChildNodes();
for(int i=0; i<list.getLength(); i++){
Node node = list.item(i);
if(node.hasChildNodes()){
Outer.pl("1:" + node.getNodeName() + ": " + node.getNodeValue() + " -- " + node.getNodeType());
Outer.pl("2:" + node.getNodeName() + ": " + node.getChildNodes().item(0).getNodeValue() + " -- " + node.getNodeType());;
}else{
Outer.pl("3"+ node.getNodeName() + ": " + node.getNodeValue() + " -- " + node.getNodeType());
}
}
}
hellolwl 2003-08-23
  • 打赏
  • 举报
回复
我用的是crimson.jar和jaxp.jar两个包
hellolwl 2003-08-23
  • 打赏
  • 举报
回复
<CGeoMLFeatureProperty>
<OID nullable="false" type="int" fractionDigits="0" totalDigits="38"/>
<AREA nullable="true" type="double"/>
<PERIMETER nullable="true" type="double"/>
<LAKE nullable="true" type="int" fractionDigits="0" totalDigits="10"/>
<LAKE_ID nullable="true" type="int" fractionDigits="0" totalDigits="10"/>
<CODE nullable="true" type="int" fractionDigits="0" totalDigits="10"/>
</CGeoMLFeatureProperty>

比如我程序已经定位到<CGeoMLFeatureProperty>这个结点
Element propetyNode = (Element)layerNode.getElementsByTagName("CGeoMLFeatureProperty").item(0);
然后我想读取OID、AREA、PERIMETER等结点名称,我采用
String propety1 = propetyNode.getChildNodes().item(0).getNodeName();
String propety2 = propetyNode.getChildNodes().item(1).getNodeName();
读出来的值是"#text",不知道是什么意思。

究竟怎么读,大家帮帮忙,急用,感激不尽!
Kylix_XP 2003-08-22
  • 打赏
  • 举报
回复
1.xml文件:

<?xml version="1.0" encoding="gb2312"?>
<total>
<item name="item 1">
<sub>sub item 1</sub>
</item>
<item name="item 2">
<sub>sub item 2</sub>
</item>
</total>
Kylix_XP 2003-08-22
  • 打赏
  • 举报
回复
/**
* 测试类, 演示如何使用JDOM对XML文档读写
*
* Jack 2003-08-22
* http://www.bflink.com
*/

package com.bflink.readandwritexml;

import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;

import java.io.*;
import java.util.List;

public class JdomSample {
public static void main(String[] args) {
SAXBuilder sb = new SAXBuilder(); // 新建立构造器
Document doc = null;
try {
doc = sb.build(new FileInputStream("1.xml"));
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (JDOMException ex) {
ex.printStackTrace();
}
// 读入1.xml

Element root = doc.getRootElement(); // 取得根节点, 就是例子中的<total>节点
List list = root.getChildren(); // 取得根节点下一层所有节点放入List类中


for (int i = 0; i < list.size(); i++) {
System.out.println("-------------------------");
Element item = (Element) list.get(i); // 取得节点实例
String name = item.getAttribute("name").getValue(); // 取得属性的值
System.out.println("NAME-->" + name);

Element sub = item.getChild("sub"); // 取得当前节点的指定子节点
String text = sub.getText(); // 取得指定子节点的内容
System.out.println("SUB-->" + text);
sub.setText("new item" + String.valueOf(i)); // 改变子节点的内容
}

Element item = (Element) list.get(0); // 取得根节点下第一个子节点

Attribute a = new Attribute("started", "true"); // 增加一个新的属性
item.addAttribute(a);
//item.setAttribute("name", "new item"); // 改变旧的属性值

String indent = ""; // 缩进符号
boolean newLines = false; // 是否产生新行
XMLOutputter outp = new XMLOutputter(indent, newLines, "gb2312"); // 构造新的输出流
try {
outp.output(doc, new FileOutputStream("2.xml"));
}
catch (IOException ex1) {
ex1.printStackTrace();
}
// 输出到2.XML文件中

}
}
Kylix_XP 2003-08-22
  • 打赏
  • 举报
回复
/**
* 测试类, 演示如何使用JDOM对XML文档读写
*
* Jack 2003-08-22
* http://www.bflink.com
*/

package com.bflink.readandwritexml;

import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;

import java.io.*;
import java.util.List;

public class JdomSample {
public static void main(String[] args) {
SAXBuilder sb = new SAXBuilder(); // 新建立构造器
Document doc = null;
try {
doc = sb.build(new FileInputStream("1.xml"));
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (JDOMException ex) {
ex.printStackTrace();
}
// 读入1.xml

Element root = doc.getRootElement(); // 取得根节点, 就是例子中的<total>节点
List list = root.getChildren(); // 取得根节点下一层所有节点放入List类中


for (int i = 0; i < list.size(); i++) {
System.out.println("-------------------------");
Element item = (Element) list.get(i); // 取得节点实例
String name = item.getAttribute("name").getValue(); // 取得属性的值
System.out.println("NAME-->" + name);

Element sub = item.getChild("sub"); // 取得当前节点的指定子节点
String text = sub.getText(); // 取得指定子节点的内容
System.out.println("SUB-->" + text);
sub.setText("new item" + String.valueOf(i)); // 改变子节点的内容
}

Element item = (Element) list.get(0); // 取得根节点下第一个子节点

Attribute a = new Attribute("started", "true"); // 增加一个新的属性
item.addAttribute(a);
//item.setAttribute("name", "new item"); // 改变旧的属性值

String indent = ""; // 缩进符号
boolean newLines = false; // 是否产生新行
XMLOutputter outp = new XMLOutputter(indent, newLines, "gb2312"); // 构造新的输出流
try {
outp.output(doc, new FileOutputStream("2.xml"));
}
catch (IOException ex1) {
ex1.printStackTrace();
}
// 输出到2.XML文件中

}
}
Dickensi 2003-08-22
  • 打赏
  • 举报
回复
方法很多getChildren().get(i);
用xpath都行

看你用哪个解析器
kjah 2003-08-22
  • 打赏
  • 举报
回复
关注

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧