/**
* 根据tagName从xml中取得取得相应Node值 格式:getNodeValueFromDom(dom,"消息/消息内容/学号")
*
* @param dom
* @param tagName
* @return
*/
public static String getNodeValueFromDom(Document dom, String tagName) {
String result = "";
try {
if (dom != null && tagName != null && !tagName.trim().equals("")) {
NodeList nodeList = null;
Node node = null;
nodeList = XPathAPI.selectNodeList(dom, tagName.trim());// dom.getElementsByTagName(tagName.trim());
if (nodeList != null && nodeList.getLength() > 0) {
/**
* xml中可能有多个tagName定义的Node,取第一个
*/
node = nodeList.item(0);
if (node != null) {
node = node.getFirstChild();
if (node != null)
result = node.getNodeValue();
result = result == null ? "" : result.trim();
}
}
}
} catch (Exception e) {
System.out.println(" getNodeValueFromDom 出错 = " + e.toString());
e.printStackTrace();
}
return result;
}
根据tagName从xml中取得取得相应Node值
最新推荐文章于 2024-05-19 21:41:52 发布
本文介绍了一种从XML文档中提取特定标签值的方法,并提供了一个Java函数实现。该函数使用XPath API来定位并获取指定路径下的节点值。
1122

被折叠的 条评论
为什么被折叠?



