XML解析器

时间:2010-05-20 04:05:06

标签: java xml android

我正在尝试使用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());                       
        }

    }

}

是否有更漂亮的方式来获取令牌?

2 个答案:

答案 0 :(得分: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));

}

答案 1 :(得分:1)

在我的情况下,虽然我的cml文档不是很大,但VTD-XML解析器服务得最好,下面给出了一些使用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());
         }
    }
相关问题