Sangeeth Saravanaraj
Sangeeth Saravanaraj

Reputation: 16627

How to store, retrieve and process XML contents in a javascript variable?

I would like to perform a regex match on some XML content in JavaScript.

  1. How do we store XML contents in a JavaScript variable?
  2. What are the effective ways to retrieve the XML content from a JavaScript variable to perform a regex match?
  3. Note: Please suggest code snippets that works in all the browsers.

Sangeeth

Upvotes: 0

Views: 4996

Answers (2)

pradeek
pradeek

Reputation: 22115

Regex is generally a bad idea for parsing XML.

Here is a solution using jQuery : XML parsing of a variable string in JavaScript

Upvotes: 1

Quentin
Quentin

Reputation: 944443

How do we store XML contents in a JavaScript variable?

That depends on where you are getting the XML from. If you get fetching it over HTTP, then XMLHttpRequest will create an XML object for you (in obj.responseXML). If you are embedded the markup in a string then I believe DOMParser is fairly well supported (and pairs with MSXML.DOMDocument in Microsoft-land)

What are the effective ways to retrieve the XML content from a JavaScript variable

DOM and XPath

to perform a regex match?

I hope you mean "to perform a regex match on text extracted from the XML". Regular expressions are a very poor tool for parsing XML.

Upvotes: 0

Related Questions