Click to See Complete Forum and Search --> : regular expression help


jessevitrone
07-14-2003, 03:04 PM
I have an xml string that I have to parse that looks like this:

<data><text>Sample Text</text><price>9.00</price></data>

I'd like to be able to pull the data from that, and have it go into an array to look like this:

data["text"] = "Sample Text";
data["price"] = "9.00";

The problem is that "text" and "price" can be anything, and any number of sub elements in the <data> tags.

I know there are ways to do this with regular expressions, but I can't remember :(

Any suggestions?

Thanks in advance.

jessevitrone
07-14-2003, 03:34 PM
answered my own question:

var dataInfo = data.match(/<(.*)>.*<\/\1>/g);
var itemXML;
var itemInfo;

for (var i = 0; i < dataInfo.length; i++) {
itemXML = dataInfo[i];
itemInfo = itemXML.match(/<(.*)>(.*)<\/\1>/);
this.data[itemInfo[1]] = itemInfo[2];
}


I split it up into 2 epxressions. The first grabs each item (eg - <text>...</text>) in the data, then parses out the tag and value from each one.