2012-02-27 15 views
0

以下はXMLファイルの一部です。私はそれに関連付けられたスコアを持っているすべてのノードを見つける必要があり、それぞれのルールが 例セットを生成するために、スコアに、すべてのSimplePredicatesがつながる:java xpathでテキストを検索する

if (GRAVH.1 <= 2751.5996775) && (WV.unity <= 93.567676535) && (Zagreb <= 74) 
{ 
score = 2.32 
} 

ここでのJavaのXPath でこれを行うことは可能ですと、XMLファイルであります..

</MiningSchema> 
     <Node id="1"> 
      <True/> 
      <Node id="2"> 
      <SimplePredicate field="GRAVH.1" operator="lessOrEqual" value="2751.5996775"/> 
      <Node id="4"> 
      <SimplePredicate field="WV.unity" operator="lessOrEqual" value="93.567676535"/> 
      <Node id="8"> 
      <SimplePredicate field="Zagreb" operator="lessOrEqual" value="74"/> 
      <Node id="16" score="2.32"> 
       <SimplePredicate field="VP.0" operator="lessOrEqual" value="6.047602111"/> 
      </Node> 
      <Node id="17"> 
       <SimplePredicate field="VP.0" operator="greaterThan" value="6.047602111"/> 
       <Node id="28" score="2.832"> 
       <SimplePredicate field="MOMI.Y" operator="lessOrEqual" value="838.9644494"/> 
       </Node> 
       <Node id="29" score="3.1075"> 
       <SimplePredicate field="MOMI.Y" operator="greaterThan" value="838.9644494"/> 
       </Node> 
      </Node> 
      </Node> 
      <Node id="9"> 
      <SimplePredicate field="Zagreb" operator="greaterThan" value="74"/> 
      <Node id="18" score="3.4"> 
       <SimplePredicate field="SP.0" operator="lessOrEqual" value="11.10848385"/> 
      </Node> 
      <Node id="19"> 
       <SimplePredicate field="SP.0" operator="greaterThan" value="11.10848385"/> 
       <Node id="30" score="3.81333333333333"> 
       <SimplePredicate field="MobCSA" operator="lessOrEqual" value="135.12"/> 
       </Node> 
       <Node id="31" score="4.04"> 
       <SimplePredicate field="MobCSA" operator="greaterThan" value="135.12"/> 
       </Node> 
      </Node> 
      </Node> 
      </Node> 
      <Node id="5"> 
      <SimplePredicate field="WV.unity" operator="greaterThan" value="93.567676535"/> 
      <Node id="10"> 
      <SimplePredicate field="VP.7" operator="lessOrEqual" value="0.583140169"/> 
      <Node id="20"> 
       <SimplePredicate field="apol" operator="lessOrEqual" value="50.9146355"/> 
       <Node id="32" score="4.48"> 
       <SimplePredicate field="MOMI.Z" operator="lessOrEqual" value="791.5388999"/> 
       </Node> 
       <Node id="33" score="4.848"> 
       <SimplePredicate field="MOMI.Z" operator="greaterThan" value="791.5388999"/> 
       </Node> 
      </Node> 
      <Node id="21" score="5.14"> 
       <SimplePredicate field="apol" operator="greaterThan" value="50.9146355"/> 
      </Node> 
      </Node> 
      <Node id="11"> 
      <SimplePredicate field="VP.7" operator="greaterThan" value="0.583140169"/> 
      <Node id="22"> 
       <SimplePredicate field="WPATH" operator="lessOrEqual" value="1502"/> 
       <Node id="34" score="5.638"> 
       <SimplePredicate field="WPATH" operator="lessOrEqual" value="1440.5"/> 
       </Node> 
       <Node id="35" score="5.45"> 
       <SimplePredicate field="WPATH" operator="greaterThan" value="1440.5"/> 
       </Node> 
      </Node> 
      <Node id="23" score="5.922"> 
       <SimplePredicate field="WPATH" operator="greaterThan" value="1502"/> 
      </Node> 
      </Node> 
      </Node> 
      </Node> 
      <Node id="3"> 
      <SimplePredicate field="GRAVH.1" operator="greaterThan" value="2751.5996775"/> 
      <Node id="6"> 
      <SimplePredicate field="ECCEN" operator="lessOrEqual" value="849"/> 
      <Node id="12"> 
      <SimplePredicate field="MOMI.Y" operator="lessOrEqual" value="8736.7661745"/> 
      <Node id="24" score="6.37"> 
       <SimplePredicate field="MOMI.R" operator="lessOrEqual" value="8.2680425545"/> 
      </Node> 
      <Node id="25" score="6.7925"> 
       <SimplePredicate field="MOMI.R" operator="greaterThan" value="8.2680425545"/> 
      </Node> 
      </Node> 
      <Node id="13" score="7.61"> 
      <SimplePredicate field="MOMI.Y" operator="greaterThan" value="8736.7661745"/> 
      </Node> 
      </Node> 
      <Node id="7"> 
      <SimplePredicate field="ECCEN" operator="greaterThan" value="849"/> 
      <Node id="14"> 
      <SimplePredicate field="WA.unity" operator="lessOrEqual" value="198.5991815"/> 
      <Node id="26" score="7.94"> 
       <SimplePredicate field="SP.3" operator="lessOrEqual" value="11.61334328"/> 
      </Node> 
      <Node id="27"> 
       <SimplePredicate field="SP.3" operator="greaterThan" value="11.61334328"/> 
       <Node id="36" score="8.75"> 
       <SimplePredicate field="MDEC.13" operator="lessOrEqual" value="6.9421166205"/> 
       </Node> 
       <Node id="37" score="8.42"> 
       <SimplePredicate field="MDEC.13" operator="greaterThan" value="6.9421166205"/> 
       </Node> 
      </Node> 
      </Node> 
      <Node id="15" score="9.408"> 
      <SimplePredicate field="WA.unity" operator="greaterThan" value="198.5991815"/> 
      </Node> 
      </Node> 
      </Node> 
     </Node> 
     </TreeModel> 
     </Segment> 
     <Segment id="3"> 
+0

この質問は非常に定義されていない '条件付きのXPath expression' – tom

+0

にグーグルしてみてください。1.提供された「XML」は、整形式のXML文書ではありません。 2.処理の結果であるべきことは何も言われていない。 ...質問を編集し、もっと意味のある情報を提供してください。そうしないと、予期せぬもので有用ではない回答につながる可能性があります。 –

+0

アイデアは、XMLファイルに格納されたデシジョンツリーに基づいてルールセット(Javaソースコード)を生成することです。ファイルが大きすぎて接続できません(90000行近く) – lochi

答えて

0

何かこれは何ですか?

//node[@score] 
0

何を試しましたか?あなたは

  • 最後の祖先には、最終的なノードを取得したノードの属性を読み取るよう

    1. は、そのNodeListの上//Node[@id='16']/ancestor::Node
    2. 反復を使用してノードの祖先を取得し、ルールセットを作成します。これは、あなたが試すことができるものですあなたがここ

    を発見したものを印刷のみ標準のJDKを使用したサンプルですが、のようなものを使用するように誘惑することができる

  • それからスコアを取得:

    public class SO9466408 { 
        private static final Map<String, String> OP = new HashMap<String, String>() {{ put("lessOrEqual", "<="); }}; 
    
        public static String attrValue(Node node, String attrName) { 
         return node.getAttributes().getNamedItem(attrName).getTextContent(); 
        } 
    
        public static void main(String[] args) throws XPathExpressionException { 
         final String id = "16"; 
         String score = null; 
         final StringBuilder ruleset = new StringBuilder("if ("); 
         // XML/XPath 
         final InputSource xmlInput = new InputSource(new URL("your file.xml").openStream()); 
         final XPath xpath = XPathFactory.newInstance().newXPath(); 
         // get the ancestors node 
         final XPathExpression expr = xpath.compile("//Node[@id='" + id + "']/ancestor::Node"); 
         final NodeList ancestors = (NodeList) expr.evaluate(xmlInput, XPathConstants.NODESET); 
    
         for (int i = 0; i < ancestors.getLength(); ++i) { 
          Node predicate = ancestors.item(i).getFirstChild(); 
          // get a new rule 
          if (predicate.getNodeName().equals("SimplePredicate")) { 
           ruleset.append(String.format("%s(%s %s %s)", i > 1 ? " && " : "", 
             attrValue(predicate, "field"), OP.get(attrValue(predicate, "operator")), attrValue(predicate, "value"))); 
          } 
          // retrieve the score on the last node 
          if (i == ancestors.getLength() - 1) { 
           score = attrValue((Node) xpath.compile("//Node[@id='" + id + "']").evaluate(ancestors.item(i), XPathConstants.NODE), "score"); 
          } 
         } 
         // show what we found 
         ruleset.append(") {\n\tscore = " + score + ";\n}"); 
         System.out.println(ruleset.toString()); 
        } 
    } 
    
    // Outputs: 
    // if ((GRAVH.1 <= 2751.5996775) && (WV.unity <= 93.567676535) && (Zagreb <= 74)) 
    // { 
    //  score = 2.32 
    // } 
    
  • +0

    アレックス、答えに感謝します。私はあなたの方法を試し、それがどうなるかを教えてくれるでしょう。 – lochi