java dom4j取得指定节点value值

土豆你个番茄 2012-05-07 03:20:24

<?xml version="1.0" encoding="UTF-8"?>
<root>
<p1>
<p2>
.
.
.
<pn>要取得的值</pn>
.
.
.
</p2>
</p1>

</root>


一个xml文件中我想取得文件中某一节点的值p62
那位高手给个例子非常感谢
...全文
535 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
土豆你个番茄 2012-05-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
document.getElementByName("p12")
我随便写的,不过大概就这么回事。
百度那么多例子,还在这问。google老被断网
等有人回复,早查到了。
[/Quote]
你这个w3c的dom 不过谢了 我搞定了
搞什么哦 2012-05-08
  • 打赏
  • 举报
回复
或者是getNodeByName,世间事大概就这么回事。
搞什么哦 2012-05-08
  • 打赏
  • 举报
回复
document.getElementByName("p12")
我随便写的,不过大概就这么回事。
百度那么多例子,还在这问。google老被断网
等有人回复,早查到了。
package com.hexiang.utils; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; /** * 本类是专门解析XML文件的,主要用于为系统读取自己的配置文件时提供最方便的解析操作 * @author HX * */ public class XmlManager { /** * 得到某节点下某个属性的 * @param element 要获取属性的节点 * @param attributeName 要取的属性名称 * @return 要获取的属性的 * @author HX_2010-01-12 */ public static String getAttribute( Element element, String attributeName ) { return element.getAttribute( attributeName ); } /** * 获取指定节点下的文本 * @param element 要获取文本的节点 * @return 指定节点下的文本 * @author HX_2010-01-12 */ public static String getText( Element element ) { return element.getFirstChild().getNodeValue(); } /** * 解析某个xml文件,并在内存中创建DOM树 * @param xmlFile 要解析的XML文件 * @return 解析某个配置文件后的Document * @throws Exception xml文件不存在 */ public static Document parse( String xmlFile ) throws Exception { // 绑定XML文件,建造DOM树 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document domTree = db.parse( xmlFile ); return domTree; } /** * 获得某节点下的某个子节点指定节点名称,和某个属性的) * 即获取parentElement下名字叫childName,并且属性attributeName的为attributeValue的子结点 * @param parentElement 要获取子节点的那个父节点 * @param childName 要获取的子节点名称 * @param attributeName 要指定的属性名称 * @param attributeValue指定的属性的 * @return 符合条件的子节点 * @throws Exception 子结点不存在或有多个符合条件的子节点 * @author HX_2008-12-01 */ public static Element getChildElement( Element parentElement, String childName, String attributeName, String attributeValue ) throws Exception { NodeList list = parentElement.getElementsByTagName( childName ); int count = 0; Element curElement = null; for ( int i = 0 ; i < list.getLength() ; i ++ ) { Element child = ( Element )list.item( i ); String value = child.getAttribute( attributeName ); if ( true == value.equals( attributeValue ) ) { curElement =

23,404

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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