【问题标题】:XML Parser in AndroidAndroid 中的 XML 解析器
【发布时间】:2023-03-26 01:53:01
【问题描述】:

我正在尝试使用 w3c dom 提取 n0Y7ezLlIYA8R0K54rEmHaTOraBQVSPDjQaGlQxlGso4jdVN1kRxtcfskEs=

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

但我似乎被卡住了

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());                       
        }

    }

}

有没有更漂亮的方式获取token?

【问题讨论】:

  • DOM 是您会考虑的唯一选择吗?
  • 拿到令牌没关系

标签: java xml android


【解决方案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));

}

【讨论】:

    【解决方案2】:

    在我的例子中,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());
             }
        }
    

    【讨论】:

    • 我在这里没有看到任何问题的答案。
    • 我想知道你怎么不认为这是一个答案,问题是获取数据 xpath 值,而这正是我的代码示例要做的事情。
    • 您只是简单地转储了代码。您还没有回答问题。该代码应该用于演示/示例。这不是答案本身。它可能是答案的一部分
    • 我在发布答案时没有看到任何指定的规则,下次会小心。而且,从问题的内容来看,我认为只需稍加修改,用户就可以得到完整的答案。
    • Read this: "How do I write a good answer" 获取一些好的指导。
    猜你喜欢
    • 2012-10-01
    • 2012-05-21
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    • 1970-01-01
    • 2011-04-30
    相关资源
    最近更新 更多