How to loop through XML-nodes and validate if values exists?

三世轮回 提交于 2021-01-29 08:09:14

问题


I have through an API fetched my data as an XML, and I wish to cycle through nodes (there are several of the same type) and add them to certain fields/a table.

Example from the XML-file:

<HistRating
  xmlns="">
  <EndrAr>2020</EndrAr>
  <EndrMnd>7</EndrMnd>
  <Rating>A</Rating>
</HistRating>
<HistRating
  xmlns="">
  <EndrAr>2019</EndrAr>
  <EndrMnd>6</EndrMnd>
  <Rating>A</Rating>
</HistRating>

I have tried the following format (at this point the XML I need is in a string in xmlDoc xmlDoc = CreateObject("MSXML2.DOMDocument.6.0"). Fully aware that this is not a really "sexy" way to write it, but I'm new at this game:

Set nodeXML = xmlDoc.getElementsByTagName("EndrAr")
Range("G1").Value = nodeXML(1).Text
Range("H1").Value = nodeXML(2).Text
Range("I1").Value = nodeXML(3).Text

Set nodeXML = xmlDoc.getElementsByTagName("EndrMnd")
Range("G2").Value = nodeXML(1).Text
Range("H2").Value = nodeXML(2).Text
Range("I2").Value = nodeXML(3).Text

Set nodeXML = xmlDoc.getElementsByTagName("Rating")
Range("G3").Value = nodeXML(1).Text
Range("H3").Value = nodeXML(2).Text
Range("I3").Value = nodeXML(3).Text

This works great as long as all three items are there. Unfortunately that is not given. If it is a new company i.e. (3) wont exist (there is one line per year above), and I would like to either set the cell to Blank or No value or something.

The result from when I run the above code:

But if I try to add a line 4 to test what happens if value does not exists I get the following (for obvious reasons)

What I would love some help with is:

  • Can I by some "magic" add a ifmissing (tried it, but could not get it to work)?
  • Other ways to add a if variable is not found, input following into cell
  • Or are there a complete different way I should have solved this?

This is to add accounting data from last X available years (where X is ie 4, or less if not 4 is available) from 30 nodes.


回答1:


Start by querying all of the HistRating elements, then loop over that collection:

Const MAX_YEARS As Long = 4
Dim ratings, rating, c As Range, i as Long

Set c= Range("A1")
Set ratings = xmlDoc.getElementsByTagName("HistRating")
For Each rating in ratings
    c.offset(0, i) = rating.getElementsByTagName("EndrAr")(0).Text
    c.offset(1, i) = rating.getElementsByTagName("EndrMnd")(0).Text
    c.offset(2, i) = rating.getElementsByTagName("Rating")(0).Text
    i = i + 1
    If i >= MAX_YEARS Then Exit For 'exit if processed enough nodes
Next rating



回答2:


You could use an Error trapping Function. Note in the code below we choose not to use the returned boolean.

Dim myTest as String
.
.
TryReadingXmlNode nodeXML,1, myText
Range("G1").Value = myText
.   
.

Public Function TryReadingXmlNode(ByVal ipNode as object, ByVal ipIndex as Long, ByRef opText as string) as boolean

    On Error Resume Next
    opText=ipNode.Item(ipIndex).Text
    TryReadingXmlNode=Len(opText)>0
    If err.number>0 then opText="NoValue"
    on Error Goto 0
 
End Function   


来源:https://stackoverflow.com/questions/64879868/how-to-loop-through-xml-nodes-and-validate-if-values-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!