Java Selenium:如何通过页面源进行解析? [英] Java Selenium: how to parse through the page source.?

查看:106
本文介绍了Java Selenium:如何通过页面源进行解析?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将以下XML作为仅输出显示在页面上.我将其作为页面源阅读,并使用文档构建器解析每个节点的值.但不幸的是,我无法读取任何值.节点列表计数仅给我零(0).

I've the following XML as only output displayed on a page. I read it as a page source and parsing through each node value using document builder. But unfortunately I'm unable to get any values being read. Count of Node list gives me zero(0) only.

下面是我的代码

String response = driver.getPageSource();

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();      
Document doc = dBuilder.parse(new InputSource(new StringReader(response)));
doc.getDocumentElement().normalize();

NodeList nList;
nList = doc.getElementsByTagName(words[0]);     //words[0]="exceptionList"
System.out.println("nList " + nList.getLength()); -- gives total length as zero
nList = doc.getElementsByTagName("exceptionList");
System.out.println("nList " + nList.getLength()); -- gives total length as zero

和XML此处供参考.

<Resultant>
    <exceptionDetails>
        <exceptionList>
            <code>ABC</code>
            <message>Invalid Value</message>
        </exceptionList>
        <exceptionList>
            <code>ABZ</code>
            <message>Invalid Structure</message>
        </exceptionList>
    </exceptionDetails>
    <Result>
        <code>1234</code>
        <Details>
            <Detail>
                <System type="A">Admin</System>
                <Type>full</Type>
                <Date>2010-02-08</Date>
            </Detail>
            <Detail>
                <System type="B">Beneficiary</System>
                <Type>full</Type>
                <Date>2015-10-05</Date>
            </Detail>
            <Detail>
                <System type="C">Customer</System>
                <Type>Partial</Type>
                <Date>2010-11-01</Date>
            </Detail>
        </Details>
    </swiftBic>
</Resultant>

我可以使用getAttribute获取类型的值,但无法获取任何节点值.请在这里帮助并纠正我要去的地方.

I can able to get the values of type using getAttribute, but unable to get any node values. Please help over here and correct me where I am going wrong.

推荐答案

尝试以下代码:

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;


public class FindElements {

    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException{

        String response =" <Resultant> " +
   " <exceptionDetails> " +
       " <exceptionList> " +
           " <code> ABC</code> " +
           " <message> Invalid Value</message> " +
       " </exceptionList> " +
       " <exceptionList> " +
           " <code> ABZ</code> " +
           " <message> Invalid Structure</message> " +
       " </exceptionList> " +
   " </exceptionDetails> " +
   " <Result> " +
       " <code> 1234</code> " +
       " <Details> " +
           " <Detail> " +
               " <System type=\"A\"> Admin</System> " +
               " <Type> full</Type> " +
               " <Date> 2010-02-08</Date> " +
           " </Detail> " +
           " <Detail> " +
               " <System type=\"B\"> Beneficiary</System> " +
               " <Type> full</Type> " +
               " <Date>2015-10-05</Date> " +
           " </Detail> " +
           " <Detail> " +
               " <System type=\"C\"> Customer</System> " +
               " <Type> Partial</Type> " +
               " <Date>2010-11-01</Date> " +
           " </Detail> " +
       " </Details> " +
   " </Result> " +
"</Resultant> " ;

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();      
        Document doc = dBuilder.parse(new InputSource(new StringReader(response)));
        doc.getDocumentElement().normalize();

        NodeList nList;
        nList = doc.getElementsByTagName("exceptionList");   
        System.out.println("nList " + nList.getLength());
        nList = doc.getElementsByTagName("Details");
        System.out.println("nList " + nList.getLength());


    }
} 

其输出如下:

nList 2
nList 1

您的xml中也有一些错误:

Also there were some errors in your xml:

</Result>丢失并且 </swiftBic>没有开始标签.

</Result> was missing and </swiftBic> had no opening tag.

这篇关于Java Selenium:如何通过页面源进行解析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆