我有以下 xml
<?xml version="1.0"?>
<response status="200">
<ns3:output xmlns="http://xmlns.oracle.com/apps/fnd/wf/worklist/service/rt/artifacts/notificationdetails/"
xmlns:ns2="http://xmlns.oracle.com/apps/fnd/wf/worklist/service/rt/artifacts/worklistmgmt/"
xmlns:ns3="http://xmlns.oracle.com/apps/fnd/wf/worklist/service/rt/artifacts/worklist/">
<ns2:notifications count="140552">
<ns2:notification>
<ns2:NotificationId>4687807</ns2:NotificationId>
</ns2:notification>
</ns2:notifications>
</ns3:output>
</response>
我需要解析它并获取每一行的 NotificationId 值。
我尝试使用下面的代码来获取列表,但它返回 0。
notificationrows = xmlDoc.documentElement.selectNodes("/ns3:output/ns2:notifications");
谁能告诉如何实现这一目标?
最佳答案
根据评论,您没有告诉我们您使用的是哪种语言/解析器,但您可以使用 local-name()
实现命名空间不可知的 xpath,避免使用 NamespaceManager
或类似,如下:
notificationrows = xmlDoc.documentElement.selectNodes("/response/*[local-name()='output']/*[local-name()='notifications']");
更新
请注意,您的根元素是
response
(在全局命名空间中),所以你要么需要明确地导航它,要么使用 //
寻找匹配的后代。
关于使用 NameSpace 解析 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14361824/