Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaSelenium:如何解析页面源代码。?_Java_Xml_Selenium Webdriver - Fatal编程技术网

JavaSelenium:如何解析页面源代码。?

JavaSelenium:如何解析页面源代码。?,java,xml,selenium-webdriver,Java,Xml,Selenium Webdriver,我将以下XML作为页面上显示的唯一输出。我将其作为页面源读取,并使用DocumentBuilder解析每个节点值。但不幸的是,我无法读取任何值。节点列表的计数仅为零(0) 下面是我的代码 String response = driver.getPageSource(); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.new

我将以下XML作为页面上显示的唯一输出。我将其作为页面源读取,并使用DocumentBuilder解析每个节点值。但不幸的是,我无法读取任何值。节点列表的计数仅为零(0)

下面是我的代码

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获取类型的值,但无法获取任何节点值。请在此提供帮助并纠正我的错误。

请尝试以下代码:

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中还存在一些错误:

丢失,并且
没有开头标记。

谢谢Kushal的回复。当我输入您提到的响应时,计数就起作用了,但是我使用selenium命令以driver.getpagesource()的形式给出响应。提供的XML仅供参考,因为它直接从视图页面源获取。你能在这里提出你的建议吗。谢谢你。。不知道。