Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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
使用JAVA获取给定XML元素的属性_Java_Xml - Fatal编程技术网

使用JAVA获取给定XML元素的属性

使用JAVA获取给定XML元素的属性,java,xml,Java,Xml,以下是XML文件: <TestCase name="SearchPromotions" type="DDTC" recovery="false" datatable="HsbcDemoSearchPromotionstestCaseSpecificVirtualDatatable" position="0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="fil

以下是XML文件:

<TestCase name="SearchPromotions" type="DDTC" recovery="false" datatable="HsbcDemoSearchPromotionstestCaseSpecificVirtualDatatable" position="0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="file://C:/New folder/VTAF/base-qa-3.0.5.2/generator/xsd/TestCase.xsd">

    <SelectedDataTableNames name="SearchData"> </SelectedDataTableNames>

    <Open page="hsbc"  ms="5000"  />
    <Click object="hsbc.Personal_Link"  />
    <Click object="hsbc.CreditCard_tab"  />
    <Call businessComponent="Global.Verify_Search">
       <Param name="HotelName_Param" value="@SearchData_link" />
    </Call>
    <CheckElementPresent object="hsbc.Img_Hotel_logo"  Identifire="Hotel_Name_PARAM:@SearchData_ResultHotelName"  fail="true"  customErrorMessage="Searched hotel name is not present in the page."  />
</TestCase>

下面是从指定元素获取属性的java代码。如果我尝试运行它,则会给出NullpointException。我不知道遗漏了什么,也不知道是什么错。谁能帮我一下吗。我已经对发生异常的行进行了注释:

File fXmlFile = new File("SearchPromotions.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();

Element docEle = doc.getDocumentElement();
NodeList nl = docEle.getChildNodes();

if (nl != null && nl.getLength() > 0) {
    for (int i = 0; i < nl.getLength(); i++) {
        if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
            Element ele = (Element) nl.item(i);

            switch(ele.getNodeName()){
                case "Click":
                    System.out.println(ele);
                    //----------------
                    System.out.println(ele.getAttributes().getNamedItem("page").getNodeValue());
                    //----------------
                    break;
                default:
                    break;
            }
        }
    }
}
File fXmlFile=新文件(“SearchPromotions.xml”);
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
documentdoc=dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
Element docEle=doc.getDocumentElement();
NodeList nl=docEle.getChildNodes();
如果(nl!=null&&nl.getLength()>0){
对于(int i=0;i
您的元素

<Click object="hsbc.Personal_Link"  />
<Click object="hsbc.CreditCard_tab"  />
必须导致一个
NullPointerException
。唯一具有“page”属性的元素是

<Open page="hsbc"  ms="5000"  />

您试图获取
单击
元素的
页面
属性,但它只包含
对象
属性


因此,当执行
ele.getAttributes().getNamedItem(“页面”).getNodeValue()
getNamedItem(“页面”)
时,将返回
null
。那是一个愚蠢的错误。我赶时间。所以我无法检查它。很抱歉谢谢如果我删掉这个问题你们会放弃投票吗?因为我觉得这个问题太愚蠢了问题从来都不愚蠢。别忘了其他人可能会提出同样的问题,并能在这里找到答案:)哦,该死。那是一个愚蠢的错误。我赶时间。所以我无法检查它。很抱歉谢谢如果我删掉这个问题你们会放弃投票吗?因为我觉得这个问题太愚蠢了
<Open page="hsbc"  ms="5000"  />