We help IT Professionals succeed at work.

XML - how to check for the existence of an element and retrieve its value if found

monkeybiz12345
on
117 Views
Last Modified: 2019-02-21
Greetings Experts!

Working with vbscript and a very limited knowledge of XML files, I'm trying to create a function that gets several pieces of information from the MaterialPosting element of an XML file.  

As long as I stick to elements that 1) are always present in the XML and 2) have values directly under the /JobInfo/MaterialPosting element, such as StockCode, I can get what I need but I'm having trouble retrieving the values of items that are one level deeper, such as Bins and Serials.  I was able to figure out how to get the value of the Bins/Bin element (see line 21 of the code below). However, it doesn't work for serials because the Serials element does not always exist.  

After spending the better part of a day with the Microsoft docs on XML Dom, my head is spinning and I still don't get it.  The code below is my latest attempt.  It returns an error of "object doesn't support this property or method" at line 11.  

How can I check to see if the element called Serials exists in my XML and, if the Serial element exists, retrieve its value?

Many thanks, in advance, for your help!



Dim objDom, objNodeList, Counter, myXML
   Set objDom = CreateObject("MSXML2.DOMDocument")
   objDom.LoadXML(XMLOut)

   Set objNodeList = objDom.SelectNodes ("//JobInfo/MaterialPosting")
       If objNodeList.length > 0 then

  ' Loop through each of the MaterialPosting elements
      For Counter = 0 To objNodeList.length - 1
          dim aSerialNode
          aSerialNode = objNodeList(Counter).SelectSingleNode("//JobInfo/MaterialPosting/Serials")
		  If aSerialNode = nothing Then
		     msgbox "no serial node"
		  Else
		     msgbox "serial node found"
		  End If
 
         MyXML = MyXML & objNodeList(Counter).SelectSingleNode("MStockCode").Text & Chr(255) &_
                 objNodeList(Counter).SelectSingleNode("MLot").Text & Chr(255) &_
	         objNodeList(Counter).SelectSingleNode("MQtyIssued").Text & Chr(255) &_
	   	 objNodeList(Counter).SelectSingleNode("./MBins/Bin").Text & vbCrLf
		   ' objNodeList(Counter).SelectSingleNode("./MSerials/Serial").Text & vbCrLf
         
      Next
   End If 

Open in new window



My XML file looks like this:

<JobInfo>
   <JobHeader>
      <Job>00006962</Job>
      <Value>          205.88</Value>
   </JobHeader>
<MaterialPosting>
   <StockCode>COMPONENT1</StockCode>
   <QtyIssued>2</QtyIssued>
   <Bins>
      <Bin>Assembly1</Bin>
      <BinQtyIssued>2</BinQtyIssued>
   </Bins>
</MaterialPosting>
<MaterialPosting>
<StockCode>COMPONENT2</StockCode>
<QtyIssued>1</QtyIssued>
<Lot>LOT6000</Lot>
<Bins>
   <Bin>Assembly1</Bin>
   <BinQtyIssued>1</BinQtyIssued>
</Bins>
<Serials>
   <Serial>1962919011</Serial>
   <SerialQtyIssued>1</SerialQtyIssued>
   <ExpirationDate/>
</Serials>
</MaterialPosting>
</JobInfo>
Comment
Watch Question

CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
This gets me going again.  Thank you zc2!
zc2
CERTIFIED EXPERT

Commented:
You are welcome!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.