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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

REGEX and JAVASCRIPT and TAGS 1

Status
Not open for further replies.

ashthud

Programmer
Oct 6, 2008
53
0
0
GB
A page on my website uses regex to read an xml file elsewhere on my site.

Example xml:
Code:
<favs>
    <title>Main</title>
    <fav>
        <title>facebook</title>
        <url>facebook.com</url>
    </fav>
    <fav>
        <title>hotmail</title>
        <url>hotmail.com</url>
    </fav>

    <title>User</title>
    <fav>
        <title>youtube</title>
        <url>youtube.com</url>
    </fav>
    <fav>
        <title>myspace</title>
        <url>myspace.com</url>
    </fav>
</favs>
What I want to be able to do is to grab text out of the <title> tags inside the <fav> tags.
Ive a while on it so far but this is my nearest working solution:

Code:
<script type="text/javascript">
var s = "";
s += "<favs>\n";
s += "  <title>TestMain</title>";
s += "	<fav>\n";
s += "	 <title>Test1</title>\n";
s += "	</fav>\n";
s += "	<fav>\n";
s += "	 <title>Test2</title>\n";
s += "	</fav>\n";
s += "	<fav>\n";
s += "	 <title>Test3</title>\n";
s += "	</fav>\n";
s += "	<fav>\n";
s += "	 <title>Test4</title>\n";
s += "	</fav>\n";
s += "</favs>\n";

var re = new RegExp([red]'((<favs>)(\s*|\S*)*)+((<fav>)((\s*|\S*)*(<\/fav>)+))*(\s*|\S*)*(^<\/favs>)'[/red], 'gi');
var matches = s.match(re);
if (matches && matches.length) {
    for (var x = 0; x < matches.length; x++) {
        document.getElementById('favourites').innerHTML += "<br>matches[" + x + "]=" + matches[x];
    }
}
</script>

I get 1 match... but it is blank so far. My regex feels too complicated (like a snowball down a steep hill:) and big.

Ashthud
 
This is embarrassing!!!!

Ive been writing loads and loads of different regex statements, all yielding blank results... and it just dawned on me:
Ive been testing my regex statements by printing my results (inc <tags>) to the innerHTML of a div...
which means that my results were being recognised as html tags (thus not being rendered).

Still, I will post my answer if I get one. =D

Ashthud
 
That! Is! Boss!

Why didn't I think of this! This solves my problem and I THANK you.

(note: im still slightly curious what the regex would be)

Ashthud
 
Oh no! XML DOM works great for every browser I have, accept CHROME!!

Code:
...
try{
 xmlDoc.async=false;
 xmlDoc.load(dname);
 return(xmlDoc);
}catch(e){
 var xmlhttp = new window.XMLHttpRequest();
 xmlhttp.open("GET",[red]"favourites.xml"[/red],false);
 xmlhttp.send(null);
 var xmlDoc = xmlhttp.responseXML.documentElement;  
 return(null);
}
...

[red]At this point[/red] chrome doesn't seem to like the local file.

Ashthud
 
One silly mistake after another... in my catch im returning null GRRR.

Ashthud
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top