【问题标题】:Getting text value from java DOM从 java DOM 获取文本值
【发布时间】:2013-04-20 09:05:09
【问题描述】:
public static void printNode(NodeList nodeList, Document d) 
{
    for (int count = 0; count < nodeList.getLength(); count++)
    {           
        Node tempNode = nodeList.item(count);
        if (tempNode.getNodeType() == Node.ELEMENT_NODE) 
        {               
            if(tempNode.getChildNodes().getLength()==1)
                {
                    if(tempNode.getNodeName()=="param-name")
                    {                               
                        tempNode = tempNode.getNextSibling();
                        System.out.println(tempNode.getTextContent());  
                    }

                }
            else if (tempNode.getChildNodes().getLength() > 1)              
                {                       
                printNode(tempNode.getChildNodes(),d);                  
                }

            else
            {
            print("ELSE")
            }
        }
    }
}

我只想从这个 xml.file 的标签中访问和获取文本值

<context-param>
    <param-name>A</param-name>
    <param-value>604800000</param-value>        
</context-param>

<context-param>
    <param-name>B</param-name>
    <param-value>50</param-value>
</context-param>

<context-param>
    <param-name>C</param-name>
    <param-value>1</param-value>        
</context-param>

但它不起作用,输出是 空白线 _空行_ 空白线 . . . .

那么,有人有想法吗?

非常感谢。

【问题讨论】:

  • 也许stackoverflow.com/questions/773012/… 可以提供帮助
  • 不要使用== 进行字符串比较。使用equals()
  • 我建议将您的问题放在帖子的顶部,以便读者在阅读代码之前了解上下文。

标签: java dom


【解决方案1】:

您似乎想要param-name 节点的兄弟值。

  1. 使用equals 方法检查对象是否相等(不是==
  2. 空白创建text nodes

考虑使用XPath

  public static void printNode(Document d) {
    try {
      NodeList values = (NodeList) XPathFactory.newInstance()
          .newXPath()
          .evaluate("//param-value/text()", d, XPathConstants.NODESET);

      for (int i = 0; i < values.getLength(); i++) {
        System.out.println(values.item(i).getTextContent());
      }
    } catch (XPathExpressionException e) {
      throw new IllegalStateException(e);
    }
  }

【讨论】:

    【解决方案2】:

    你可能想尝试这样的事情:

    public static void printNode(NodeList nodeList, Document d) {
    
      for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
    
        if (node instanceof Element) {
          Element outerElement = (Element) node;
          NodeList paramElements = outerElement
              .getElementsByTagName("param-name");
    
          if (paramElements.getLength() != 1) {
            throw new RuntimeException("Wish I wasn't doing this by hand!");
          }
    
          Element element = (Element) paramElements.item(0);
          System.out.println(element.getTextContent());
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-09-18
      • 2010-10-20
      • 1970-01-01
      • 2012-04-19
      • 2013-02-15
      • 1970-01-01
      • 2023-04-04
      • 2012-05-01
      • 1970-01-01
      相关资源
      最近更新 更多