2011-05-02 79 views
1

HI there, 我有一個動態生成的xml,我想從中提取信息。我使用的是Xpath表達式,但發現我的xpath表達式工作不正常,它不能正常工作「T選擇什麼.....這是我的Java函數執行該XPath表達式:Xpath表達式不會選擇任何東西

public void TraverseClass(NavigationTree parent,String name) 
    { 
     xPath = XPathFactory.newInstance().newXPath(); 
      XPathExpression expr; 

      try { 
       expr = xPath.compile("Class[@name=\""+name+"\"]/Class"); 
       Object result = expr.evaluate(document, XPathConstants.NODESET); 
        NodeList nodes = (NodeList) result; 

        JOptionPane.showMessageDialog(null,nodes.getLength()); 
        for(int i=0;i<=nodes.getLength()-1;i++) 
        { 
         Node first=nodes.item(i); 
         Element node=(Element)first; 
         JOptionPane.showMessageDialog(null,"Found Classes inside "+name+" " +node.getAttribute("name")); 
         parent.foundClass(node.getAttribute("name")); 
        //Interfaces[i]=node.getAttribute("name"); 

        } 
        expr = xPath.compile("Class[@name=\""+name+"\"]/Interfaces"); 
        result = expr.evaluate(document, XPathConstants.NODESET); 
        nodes = (NodeList) result; 

         JOptionPane.showMessageDialog(null,nodes.getLength()); 
         for(int i=0;i<=nodes.getLength()-1;i++) 
         { 
          Node first=nodes.item(i); 
          Element node=(Element)first; 
          JOptionPane.showMessageDialog(null,"Found Interfaces inside "+name+" "+node.getAttribute("name")); 
          parent.foundInterface(node.getAttribute("name")); 
         //Interfaces[i]=node.getAttribute("name"); 

         } 
         expr = xPath.compile("Class[@name=\""+name+"\"]/Method"); 
         result = expr.evaluate(document, XPathConstants.NODESET); 
         nodes = (NodeList) result; 

          JOptionPane.showMessageDialog(null,nodes.getLength()); 
          for(int i=0;i<=nodes.getLength()-1;i++) 
          { 
           Node first=nodes.item(i); 
           Element node=(Element)first; 
           JOptionPane.showMessageDialog(null,"Found Methods inside "+name+" "+node.getAttribute("name")); 
           parent.foundMethod(node.getAttribute("name")); 
          //Interfaces[i]=node.getAttribute("name"); 

          } 


      } catch (XPathExpressionException e) { 
       JOptionPane.showMessageDialog(null,"error: "+e.toString()); 
       e.printStackTrace(); 
      } 
      JOptionPane.showMessageDialog(null,"going one step back in Class "+name); 
      parent.goOneStepBack(); 
    } 

現在我試圖解析XML文件

<?xml version="1.0"?> 
<program> 
<Interface modifier="public" name="interface1"> 
    <ParentInterface> i1</ParentInterface> 
    <ParentInterface> i2</ParentInterface> 
    <ParentInterface> i3</ParentInterface> 
    <Field modifier="" type="int" name="a,b,c"> 
    </Field> 
    <Interface modifier="public" name="one"> 
    <ParentInterface> two</ParentInterface> 
    </Interface> 
    <Class modifier="public" name="three"> 
    <Parent>four</Parent> 
    </Class> 
    <Method modifier="" type="void" name="foo1"> 
    <FormalParameter > 
    </FormalParameter> 
    </Method> 
</Interface> 
<Class modifier="" name="FrontPAge"> 
    <Parent>JFrame</Parent> 
    <Field modifier="public" type="int" name="a,b,c"> 
    </Field> 
    <Field modifier="" type="int" name="arr[]"> 
    </Field> 
    <Field modifier="" type="int" name="arr2[]"> 
    </Field> 
    <Field modifier="" type="int" name="num[]"> 
    </Field> 
    <Field modifier="" type="FrontPAge" name="fp"> 
    </Field> 
    <Constructor modifier="public" name="FRontPage"> 
    <FormalParameter > 
    </FormalParameter> 
    <Statement> <![CDATA[System.out.println("It is a constructor")]]></Statement> 
    </Constructor> 
    <Method modifier="publicstatic" type="void" name="main"> 
    <FormalParameter modifier="" type="String[]" var_name="args" > 
    </FormalParameter> 
    <Field modifier="" type="int" name="x,y,z"> 
    </Field> 
    <Field modifier="" type="int" name="sum[]"> 
    </Field> 
    <For> 
    <Itr_Var type="int">x</Itr_Var> 
    <Collection><![CDATA[num]]></Collection> 
    <Statement> <![CDATA[sum+=x]]></Statement> 
    </For> 
    <Interface modifier="public" name="qqq"> 
    <ParentInterface> www</ParentInterface> 
    </Interface> 
    <Statement> <![CDATA[System.out.println("Runnable is gonna start")]]></Statement> 
    <Class name="Runnable"> 
    <Method modifier="public" type="void" name="run"> 
    <FormalParameter > 
    </FormalParameter> 
    <Statement> <![CDATA[System.out.println("Hello")]]></Statement> 
    </Method> 
    </Class> 
    <Statement> <![CDATA[EventQueue.invokeLater(newRunnable(){publicvoidrun() {System.out.println("Hello");}})]]></Statement> 
    <Statement> <![CDATA[System.out.println("Runnable Ends Here")]]></Statement> 
    <Statement> <![CDATA[c=b+d]]></Statement> 
    <Statement> <![CDATA[a=b+c]]></Statement> 
    </Method> 
    <Class modifier="" name="nested"> 
    </Class> 
    <Interface modifier="public" name="xxx"> 
    <ParentInterface> yyy</ParentInterface> 
    </Interface> 
</Class> 
</program> 

信息:JAVA的名稱參數函數包含一個最高級別的名稱l類,也就是根元素的後代,它可能還包含其他類/接口/方法...

問題:我的問題是我使用的表達式Class[@name=<ClassName>]/Class應該從xml中選擇所有類,排除除select帶有名稱的類,然後選擇該類的所有子類......與接口和方法相同...但它根本不起作用!!!! ....表達式都不匹配任何....

得多,因爲從源代碼動態生成xml和除了根元素太經受改變

+0

您忘了告訴我們您要選擇提供的XML文檔的哪些節點?請這樣做,否則您的問題不是真正的問題,可能會被關閉/刪除。不要說人們猜測,你收到的答案不是及時或準確的。 – 2011-05-02 13:04:49

回答

3

該表達式選擇所有嵌套類(只有一個示例文檔中:類「頭版」有着嵌套類:

//Class/Class 

或name屬性

//Class[@name="FrontPAge"]/Class 

你表達應該工作,如果你評估根節點而不是文件(8號線)。如果要對文檔評估,第一個元素是<program>,你不得不說

program/Class[@name=\"" + name + "\"]/Class 

關的記錄:我以前this online xpath evaluator驗證表達式。只需將示例文檔複製到文本字段並使用XPath表達式進行播放

2

我薄從而內容的XML的我不能依靠絕對路徑K優可能需要用program啓動XPath表達式,因爲它是根元素

如:

expr = xPath.compile("program/Class[@name=\""+name+"\"]/Interfaces") 

我沒有測試過這一點我自己,但它是值得一試:)

+0

'* /'也適用於根元素。這是我們通常做xpath的方式。 – 2011-05-02 07:46:16

+0

我試過了,但它不起作用... :( – Sudh 2011-05-02 07:47:30

+0

我不明白,但是當我改變我的表情到 expr = xPath.compile(「program // Class [@ name = \」「 +名稱+「\」] /接口「); 它至少爲這個XML工作....現在當我解釋它時,它會選擇程序的所有後代並選擇具有給定名稱的類元素,然後選擇該類的所有接口.....但是我懷疑當有兩個高級類時,它會失敗包含具有相同名稱的其他建議的接口,以獲得更好的XPath表達式? – Sudh 2011-05-02 07:59:33