xml文件如下:
xml文件
我写的代码:
列表查询xmlusingxpathandreturnlist(string xml,string xpathexpression){
documentbuilderfactory dbfactory=documentbuilderfactory.newinstance()
documentbuilder dbuilder=dbfactory.newdocumentbuilder()
document doc=dbuilder.parse(new bytearrayinputstream(xml.getbytes(standardcharsets.utf8)))
doc.getdocumentelement().normalize()
xpath=xpathfactory.newinstance().newxpath()
nodelist nodelist=(nodelist)xpath.compile(xpathexpression).evaluate(doc,xpathconstants.nodeset)
list returnelements=new arraylist<>()
nodelist.each{n->
returnelements.add(n.gettextcontent())
}
当我将xpath传递为:
/信封/body/commandresponsedata/operationresult/operation/parameterlist/listparameter/stringelement
它返回所有值。但是我只想返回name=“packagetypelist”的listparameter值。
为此,我将xpath用作:
/信封/body/commandresponsedata/operationresult/operation/parameterlist/listparameter[@name='packagetypelist']/stringelement
但它将list返回为null。
1条答案
按热度按时间6yt4nkrj1#
我猜您在xpath表达式中遗漏了“commandresponsedata”和“operationresult”之间的“commandresult”。