代码之家  ›  专栏  ›  技术社区  ›  darthblonderss

XPath获取所有子节点和值

  •  0
  • darthblonderss  · 技术社区  · 7 年前

    我试图从以下XML源中获取所有活动及其子节点。

    <process>
      <name>processName</name>
      <input>JSON</input>
      <input>productName</input>
      <output>JSON</output>
      <activity>
        <name>createDomain</name>
        <type>bean</type>
        <input>domainName</input>
        <output>JSON</output>
      </activity>
      <mapActivity>
        <map></map>
      </mapActivity>
      <activity>
        <name>preFill</name>
        <type>REST</type>
        <input>JSON</input>
        <output>JSON</output>
      </activity>
    </process>
    

    createDomain, bean, domainName, JSON preFill, REST, JSON, JSON . 但我不确定该怎么做。

    我目前有这个代码。

        public static void displayActivities(Document myDoc) throws Exception {
        XPathExpression exp = xp.compile("process/activity/*");
        NodeList myList = (NodeList) exp.evaluate(myDoc, XPathConstants.NODESET);
        for (int i = 0; i < myList.getLength(); i++) {
            Node tempNode = myList.item(i);
            System.out.println("Value: " + tempNode.getNodeValue());
            if (tempNode.hasChildNodes()) {
                for (int x = 0; x < tempNode.getChildNodes().getLength(); x++) {
                    System.out.println("Sub-value: " + myDoc.getChildNodes().item(x).getNodeName());
                }
            }
        }
        System.out.println("------------------------------");
    }
    

    这将返回以下内容:

    Value: null
    Sub-value: process
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   dblanken    7 年前

    • <
    • <类型(>)</类型(>);
    • <输入(>);JSON</输入(>);
    • <输出(>);JSON</输出(>);

    Node Javadoc 当调用getNodeValue()时,元素将返回null。