xml元素中不常存在节点

时间:2022-03-22 05:30:54
 <ObjectArrayToXml xmlns="http://www.something.com/">
      <itemslist>
        <Size>18</Size>
        <ItemInfo>
          <Pass>5225</Pass>
          <AgentName>aaaaaa</AgentName>
          <Document>6763</Document>
        </ItemInfo>
        <ItemInfo>
          <Pass>1223</Pass>
          <Document>77755</Document>
**here the tag of AgentName will be missing
        </ItemInfo>
        <ItemInfo>
          <Pass>45344</Pass>
          <AgentName>bbbbbbb</AgentName>
          <Document>22234</Document>
        </ItemInfo>
      </itemslist>
    </ObjectArrayToXml>

we are getting the above structure with all 3 tags Pass,AgentName,Document for each item for more than 4 years we been pharsing and printing it successfully like that:

我们正在使用所有3个标签Pass,AgentName,Document为每个项目获得上述结构超过4年我们已经成功并且成功打印它:

Set NodeList = xmlDoc.documentElement.selectNodes("//ObjectArrayToXml/itemslist/ItemInfo")
     For Each Node In NodeList
response.write Node.selectSingleNode("Pass").text
response.write Node.selectSingleNode("AgentName").text
response.write Node.selectSingleNode("Document").text   
     Next  

the structure of xml was changed and now we are not always getting the tag of AgentName - it might not come at all for some of items. I need to check now if tag exist for each item, then i wrote this code:

xml的结构已经改变,现在我们并不总是得到AgentName的标签 - 它可能根本不适用于某些项目。我现在需要检查每个项目是否存在标签,然后我写了这段代码:

set xxx = xmlDoc.selectSingleNode("//AgentName")
If Not xxx Is Nothing Then
response.write Node.selectSingleNode("AgentName").text
End If

it works great if ALL items has this tag or if ALL items doesn't have this tag. BUT once some items have this tag and others ain't then page crashes and i gives 500 error. I realized that xmlDoc.selectSingleNode("//AgentName") must looking the first item only and assume that all the rest items has same taga, and not checking each and every one of items for existance of AgentName tag. I might not be right, can you advice how to check if this tag exist in each item?

如果所有项目都有此标记或者所有项目都没有此标记,则效果很好。但是,一旦某些项目有这个标签而其他项目没有,那么页面崩溃,我给出500错误。我意识到xmlDoc.selectSingleNode(“// AgentName”)必须只查看第一个项目,并假设所有其余项目具有相同的taga,并且不检查每个项目是否存在AgentName标记。我可能不对,你能建议如何检查每个项目中是否存在这个标签?

1 个解决方案

#1


0  

Check AgentName of current Node in every iteration, try something like this inside the For Each loop :

在每次迭代中检查当前Node的AgentName,在For Each循环中尝试类似这样的事情:

set agentName = Node.selectSingleNode("AgentName")
If Not agentName Is Nothing Then
    response.write agentName.text
End If

#1


0  

Check AgentName of current Node in every iteration, try something like this inside the For Each loop :

在每次迭代中检查当前Node的AgentName,在For Each循环中尝试类似这样的事情:

set agentName = Node.selectSingleNode("AgentName")
If Not agentName Is Nothing Then
    response.write agentName.text
End If