Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reg Expresion to parse XML??

Status
Not open for further replies.

ChrisQuick

Programmer
Oct 4, 1999
144
US
I have an application that is returning database information as XML. For example:

<FIELDS NAME=&quot;SMITH&quot; ADDRESS=&quot;123 SOME ST&quot;/>
<FIELDS NAME=&quot;JONES&quot; ADDRESS=&quot;778 SOMEOTHER RD&quot;/>

I am trying to write a javascript function that will pull all of the Address values into an JS array.

What I have so far (and that doesn't work) is:

function parseAddresses(theReply) {
var re = new RegExp(&quot;ADDRESS=+&quot;)
var testTest=&quot;&quot;;
testTest = re.exec(theReply);
}

Can anyone help?
cquick@callingpost.com
Geographic Information System (GIS), ASP, some Oracle
 
I don't think that is what I am looking for. cquick@callingpost.com
Geographic Information System (GIS), ASP, some Oracle
 
Actually, if you are developing for an HTML4-compliant browser, such as IE5+ or Netscape6/Mozilla, you don't need a Regex at all to deal with this, since each of these browsers has native XML processing built in. Unfortunately, I have not spent any time on these features yet, so I can't help you directly, but if you spend some time researching browser-based XML, or perhaps XHTML, you should be able to find an answer to this.
 
The XML is being returned to a hidden frame not for direct display. I need to read the addresses out in order to pass them to new window. cquick@callingpost.com
Geographic Information System (GIS), ASP, some Oracle
 
Does anyone else have some suggestions?? Note: I am NOT interested in displaying the XML to the user in anyway what so ever. I will be passing it to a new window in order to build a form with the values and pass it to another website. cquick@callingpost.com
Geographic Information System (GIS), ASP, some Oracle
 
ChrisQuick,

Do you have any control over how the XML is sent to the browser? In other words, does it have to be in a frame? Can it be output in any place you want into the HTML page, or is it coming from somewhere that you have no control over?

When you say you want to build a form with the values, where will that form be built? What server environment are you working in, and do you have any control over that?

How about showing us a sample of your XML.
 
ChrisQuick

At the moment, your regexp is wrong.
Code:
/ADDRESS=+/
matches
Code:
'ADDRESS'
followed by one or more
Code:
'='
signs, eg:
Code:
'ADDRESS====='

A better regexp would be
Code:
/ADDRESS=(.+)/

Coming from perl, I'm more used to the following statements, but I'm sure you can use the exec to do something similar:
Code:
var result = theReply.match( /ADDRESS=(.+)/ );
if( result != null ) {
    alert( &quot;Address is &quot; + result[1] );
}
Cheers, NEIL
 
No I don't have any control over how the XML is received. Actually the Entire response I get includes the XML response from the server and a form for making a new xml request like this:


<META HTTP-EQUIV=&quot;Content-Type&quot; CONTENT=&quot;text/html; charset=ISO-8859-1&quot;>
<HTML><HEAD><TITLE>Default Form</TITLE>
<!-- Title must match jsForm.htm's title -->
<SCRIPT TYPE=&quot;text/javascript&quot; LANGUAGE=&quot;JavaScript&quot;>
function passXML() {

var XMLResponse='<?xml version=&quot;1.0&quot; encoding=&quot;UTF8&quot;?><ARCXML version=&quot;1.1&quot;><RESPONSE><FEATURES><FEATURE><FIELDS NAME=&quot;SMITH&quot; ADDRESS=&quot;123 SOME ST&quot;/></FEATURE><FEATURE>
<FIELDS NAME=&quot;JONES&quot; ADDRESS=&quot;778 SOMEOTHER RD&quot;/></FEATURE></FEATURES></RESPONSE></ARCXML>';

parent.MapFrame.processXML(XMLResponse);

}
</SCRIPT>
</HEAD>
<BODY BGCOLOR=&quot;#000000&quot; onload=&quot;passXML()&quot;>
<FORM ACTION=&quot;&quot; METHOD=&quot;POST&quot; name=&quot;theForm&quot;>
<INPUT TYPE=&quot;Hidden&quot; NAME=&quot;ArcXMLRequest&quot; VALUE=&quot;&quot;>
<INPUT TYPE=&quot;Hidden&quot; NAME=&quot;JavaScriptFunction&quot; VALUE=&quot;parent.MapFrame.processXML&quot;>
<INPUT TYPE=&quot;Hidden&quot; NAME=&quot;BgColor&quot; VALUE=&quot;#000000&quot;>
<INPUT TYPE=&quot;Hidden&quot; NAME=&quot;FormCharset&quot; VALUE=&quot;ISO-8859-1&quot;>
<INPUT TYPE=&quot;Hidden&quot; NAME=&quot;RedirectURL&quot; VALUE=&quot;&quot;>
<INPUT TYPE=&quot;Hidden&quot; NAME=&quot;HeaderFile&quot; VALUE=&quot;&quot;>
<INPUT TYPE=&quot;Hidden&quot; NAME=&quot;FooterFile&quot; VALUE=&quot;&quot;>
</FORM>
</BODY>
</HTML>

the process processXML function does a case switch on a global var to figure out what function to pass the XML to.

The &quot;out of the box&quot; function that parses the XML uses about 300 lines of code to read through XML response and write the information to another frame in a HTML table BUT it is extremely cluncky code and if a large amount of XML is return it hangs Netscape. I thought that by using reg expressions, I could more easily grab the data I needed.

cquick@callingpost.com
Geographic Information System (GIS), ASP, some Oracle
 
also you could have used xsl that is especially designed to transform xml in html client-side, and very easy to use, and widely supported (appart from nn<6, as usual), and very light
or any server side language (such as perl which has nice xml modules, or php, or whatever)
 
The servet supports several development environments, so i've changed the XML request and submitted it to the application using ASP and VBScript instead. cquick@callingpost.com
Geographic Information System (GIS), ASP, some Oracle
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top