cancel
Showing results for 
Search instead for 
Did you mean: 

How to parse an XML using JavaScript

Contributor

How to parse an XML using JavaScript

Here is my xml

<?xml version="1.0" encoding="UTF-8"?>
<BasicExport xmlns="http://www.in.casds.com/SSR/XTI/Traffic/0011" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.uk.nds.com/SSR/XTI/Traffic/0011 0011_export.xsd" transactionIdentifier="585073235185">
	<CaProductList>
		<CaProductItem>
			<caProductId>00000007832</caProductId>
			<productType>IPPV and OPPV</productType>
			<productTypeId>3</productTypeId>
			<productStartDateTime>2013/09/10 19:30:24:00</productStartDateTime>
			<productExpiryDateTime>2038/04/22 00:00:00:00</productExpiryDateTime>
		</CaProductItem>
		<CaProductItem>
			<caProductId>00000007832</caProductId>
			<productType>XTV and OPPV</productType>
			<productTypeId>6</productTypeId>
			<productStartDateTime>2013/09/10 19:30:24:00</productStartDateTime>
			<productExpiryDateTime>2038/04/22 00:00:00:00</productExpiryDateTime>
		</CaProductItem>
		<CaProductItem>
			<caProductId>00000007833</caProductId>
			<productType>IPPV and OPPV</productType>
			<productTypeId>3</productTypeId>
			<productStartDateTime>2013/09/10 23:04:07:00</productStartDateTime>
			<productExpiryDateTime>2038/04/22 00:00:00:00</productExpiryDateTime>
		</CaProductItem>
		<CaProductItem>
			<caProductId>00000007833</caProductId>
			<productType>XTV and OPPV</productType>
			<productTypeId>6</productTypeId>
			<productStartDateTime>2013/09/10 23:04:07:00</productStartDateTime>
			<productExpiryDateTime>2038/04/22 00:00:00:00</productExpiryDateTime>
		</CaProductItem>
	</CaProductList>
</BasicExport>

I would like to retrieve a value for caProductId[1]

Thanks

8 REPLIES 8
Community Hero

Re: How to parse an XML using JavaScript

What I've done in the past is create an instance of the MSXML2.DOMDocument object and do my parsing and examination that way.  There are methods for "GetNode" and such built into that object that allows you to return all child nodes of a particular search pattern as an array that you can then navigate or reference by index.

See https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ms754523%28v%3dvs.85%29


Robert Martin
[Community Expert Group]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Extensions available
Contributor

Re: How to parse an XML using JavaScript

Sorry

The link seems to be broken. Would you be able to post your solution here please?

 

Thanks

Community Manager

Re: How to parse an XML using JavaScript

Hi @pravin_k99,

There is a great tutorial in the TestComplete documentation explaining how to work with XML files from script. Take a loot at it:

https://support.smartbear.com/testcomplete/docs/testing-with/advanced/working-with-external-data-sou...

---------
Tanya Gorbunova
SmartBear Community Manager

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
Community Hero

Re: How to parse an XML using JavaScript


@pravin_k99 wrote:

Sorry

The link seems to be broken. Would you be able to post your solution here please?

 

Thanks


The link is corrected... the page referenced gives some examples in JScript code.   The link @TanyaGorbunova provided gives some code examples as well.  I don't have the code at my fingertips... that was at a different job so not immediately available and, unfortunately, I don't have time to reinvent it...


Robert Martin
[Community Expert Group]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Extensions available
Contributor

Re: How to parse an XML using JavaScript

Thanks Robert and Tanya

 

I wrote this piece of code but unfortunately, it works only when I remove namespaces from the XML.

<BasicExport xmlns="http://www.in.casds.com/SSR/XTI/Traffic/0011" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.uk.nds.com/SSR/XTI/Traffic/0011 0011_export.xsd" transactionIdentifier="585073235185">

REPLACED with

<BasicExport>

 

function FirstValue()
{
  var Doc, s, Nodes, ChildNodes, i, Node;
  Doc = getActiveXObject("Msxml2.DOMDocument.6.0");
  Doc.async = false;
  Doc.load("C:\\mytest.xml");

  if(Doc.parseError.errorCode != 0)
  {
    s = "Reason:\t" + Doc.parseError.reason + "\n" +
        "Line:\t" + aqConvert.VarToStr(Doc.parseError.line) + "\n" + 
        "Pos:\t" + aqConvert.VarToStr(Doc.parseError.linePos) + "\n" + 
        "Source:\t" + Doc.parseError.srcText;
    Log.Error("Cannot parse the document.", s); 
    return;
  }
  
  Nodes = Doc.selectNodes("//caProductId");
  Log.Message(Nodes.item(1).childNodes.item(1).text); 

}

 

Though there is an example for namespace based XML. My XML seems to have namespace only in the first tag and then the rest of the xml doesnot use the namespace.

 

Can you please help?

Community Manager

Re: How to parse an XML using JavaScript

Hi,

What do you mean when you say " the code doesn't work"? Do you see any errors/exceptions? Please post them here.

 

Also, it's worth seeing your xml file (at least the structure of the file). Could you please post it here?

---------
Tanya Gorbunova
SmartBear Community Manager

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
Community Manager

Re: How to parse an XML using JavaScript

Hi @pravin_k99,

 

Was the question resolved? Do you mind posting the solution you've found here?

---------
Tanya Gorbunova
SmartBear Community Manager

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
Community Hero

Re: How to parse an XML using JavaScript

Hi,

 

> it works only when I remove namespaces from the XML.

Try this:

a)

  oXml = Sys.OleObject("Msxml2.DOMDocument");
  oXml.async = false;
  oXml.setProperty("SelectionLanguage", "XPath");

and

b)

// function returns namespace-agnostic form of the searched parameter
// From: http://jcooney.net/archive/2005/08/09/6517.aspx
function GetNamespaceAgnosticXPathClause(strClause)
{
  return aqString.Format(".//*[local-name()=\"%s\"]", strClause) // search recursively from the current node
}

Sample usage:
strValue = "//" + GetNamespaceAgnosticXPathClause("caProductId");
Nodes = Doc.selectNodes(strValue);

Does the above help?

 

 

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Postings made by [Community Expert Group] members
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
New Here?
Join us and watch the welcome video: