2010-05-20 128 views
1

我试图使用W3C DOMXML解析器

<html> 
<div id='token' style='display:none;'> 
n0Y7ezLlIYA8R0K54rEmHaTOraBQVSPDjQaGlQxlGso4jdVN1kRxtcfskEs= 
</div> 
</html> 

提取n0Y7ezLlIYA8R0K54rEmHaTOraBQVSPDjQaGlQxlGso4jdVN1kRxtcfskEs=但我似乎被卡住

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
Document doc = builder.parse(con.getInputStream()); 
NodeList list = doc.getElementsByTagName("div"); 

有人请点我的一些基本教程,帮助我解决我的困境。 谢谢。

编辑:

好吧,我得到它的工作,但它似乎笨重

String token; 
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
Document doc = builder.parse(con.getInputStream()); 
Element root = doc.getDocumentElement(); 
NodeList items = root.getElementsByTagName("html"); 

for(int i = 0; i < items.getLength(); i++) { 

    Message message = new Message(); 
    Node item = items.item(i); 
    NodeList properties = item.getChildNodes(); 

    for(int j = 0; j < properties.getLength(); j++) { 
     Node property = properties.item(j); 
     String name = property.getNodeName(); 

     if(name.equalsIgnoreCase("div")) { 
      token = property.getFirstChild().getNodeValue());      
     } 

    } 

} 

一点点有一个漂亮的方式来获得令牌?

+2

你怎么卡? – 2010-05-20 04:07:58

+0

好吧我编辑了一下 – lemon 2010-05-20 04:22:38

+0

DOM是你会考虑的唯一选择吗? – 2010-05-20 05:49:56

回答

1
VTDGen vg= new VTDGen(); 

if (vg.parseFile("input.xml",false)){ 
    VTDNav vn = vg.getNav(); 
    vn.toElement(VTDNav.FIRST_CHILD); 
    int i = vn.getText(); 
    if (i!=-1) 
    System.out.println(" text node is "+vn.toString(i)); 

} 
+0

好吧我会检查VTD。 – lemon 2010-05-24 02:46:33

1

在我的情况VTD-XML解析器服务的最好的,虽然我的CML文件都不是巨大的,利用VTD-XML一些示例代码如下所示。您可以参考以下链接,它们解释了VTD-XML解析器比SAX,DOM等解析器更好,因为它们也提供了性能基准测试。 http://www.codeproject.com/Articles/28237/Programming-XPath-with-VTD-XML http://www.codeproject.com/Articles/24354/VTD-XML-XML-Processing-for-the-Future-Part-II

//对于阅读的XPath值

public String readXpathValue(String dir, String file, String xpath) { 
     String value = null; 
     try{ 

      VTDGen vg = new VTDGen(); 
      int i; 
      AutoPilot ap = new AutoPilot(); 
      ap.selectXPath(xpath); 
      if (vg.parseFile(dir+file, true)) 
      { 
       VTDNav vn = vg.getNav(); 
       ap.bind(vn); 
       //XPath eval returns one node at a time 
       while ((i = ap.evalXPath()) != -1) 
       { 
        value = vn.toString(i); 
       } 
       // ap.resetXPath(); 

      } 
     } 
     catch (Exception e){ 
      System.out.println("Exception Occurred in reading Xpath Value : "+e); 
     } 
     return value; 

    } 

//在运行时修改xml文件

public void formCreateXMLRequest(MAMData mamData,Map<String, String> strTobeModified) throws DatatypeConfigurationException, PayPalUserCreationFailedException, ModifyException, TranscodeException, IOException, XPathEvalException, NavException, XPathParseException 
    { 
VTDGen vg = new VTDGen(); 
     if (!vg.parseFile(mamData.getDirectory() + mamData.getBatchRequest(), true)) 
      return; 
     VTDNav vn = vg.getNav(); 
     XMLModifier xm = new XMLModifier(vn); 
     AutoPilot ap = new AutoPilot(vn); 

     Set<String> xpathkeys= strTobeModified.keySet(); 
     for(String xpath : xpathkeys) { 


     ap.selectXPath(xpath); 
     while((ap.evalXPath()) != -1) 
      { 
       int p = vn.getText(); 
       xm.updateToken(p, strTobeModified.get(xpath)); 
      } 

      xm.output(mamData.getDirectory()+mamData.getBatchRequest()); 
     } 
    } 
+0

我在这里看不到任何问题的答案。 – 2014-08-26 19:05:16

+0

我可以知道你怎么看不到这个答案,问题是要获得数据xpath值,这就是我的代码示例所要做的。 – akshaymani 2014-08-27 07:06:21

+0

您只需转储代码。你没有*回答问题*。代码应该用于演示/示例。这不是答案本身。它可能是答案的一部分。 – 2014-08-27 13:32:56