Welcome to WebmasterWorld Guest from 54.234.114.182

Forum Moderators: open

Message Too Old, No Replies

Using javascript instead of regex

on .xml file

     
6:22 am on Oct 11, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:May 8, 2004
posts: 130
votes: 0


I've not tried programming using javascript before and need some help in this area. :) There are 2 questions that are somewhat related.

1) I am outputting an .xml file that has the line:

<img alt="wigget" src="http://example.com/wigget/red-wigget.jpg" width="100" height="120" class="class1">

Q: How do I use javascript remove all the tags and attributes and change it to:

http://example.com/wigget/red-wigget.jpg

2) On the same .xml file, I have a chunk of text which is stored in a tag from movable type:

...........
<form>
...........
<input type="hidden" name="amount" value="19.99">
</form>

Q: How do I use javascript to extract the 19.99 and replace the entire chunk of text with 19.99?

Thank for all the help rendered!

2:39 pm on Oct 11, 2007 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts:4988
votes: 12


Sounds like you'd want something like this:

function replaceNodeWithAttrValue(newValue, refNode) {
var parent = refNode.parentNode;
parent.replaceChild(document.createTextNode(newValue), refNode);
}

It would help if you added id attributes to your nodes. For example:

<img alt="wigget" src="http://example.com/wigget/red-wigget.jpg" width="100" height="120" class="class1" id="redwigget">
<form id="formX">
<input type="hidden" name="amount" value="19.99">
</form>

Then you could do:

window.onload = function() {
var img = document.getElementById('redwigget');
replaceNodeWithAttrValue(img.src,img);
var frm = document.getElementById('formX');
replaceNodeWithAttrValue(frm['amount'].value, frm);
};

Note, I haven't tested any of this, and I'm not sure what context you're running it in (a web page?). Hope it helps though.

2:45 pm on Oct 11, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Mar 29, 2007
posts:1147
votes: 0


Using javascript instead of regex

This title seems strange, why not use a JavaScript regex? The syntax is a little different than whatever you've used, Perl?

Try this, it's untested but should work. If you're familliar with regex's you should recognise the syntax anyway. Note I've changed your double quotes to single for the purpose of containing it within another string.


var text="<img ... src='http://example.com/wigget/red-wigget.jpg' ...>";
text.replace( /^.*src='(.*?)'.*$/, "$1");
alert( text);

You can easily change src to value for the second one, or if you need to use it a lot, use a universal function to extract the parameter you want.

2:46 pm on Oct 11, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Mar 29, 2007
posts:1147
votes: 0


Sorry, cross-posted!

Fotiman's example will work if the elements are actually on the page, depends if you're putting your XML straight into the document body, or if you're holding it in a string at the time.

3:47 pm on Oct 12, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Mar 29, 2007
posts:1147
votes: 0


Sorry, my example won't work, I'm still thinking in Perl!

This should:

var text="<img ... src='http://example.com/wigget/red-wigget.jpg' ...>"; 
var srcString = text.replace( /^.*src='(.*?)'.*$/, "$1");
alert( srcString);
4:59 pm on Oct 12, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:May 8, 2004
posts: 130
votes: 0


Thank you for all your help. But as I'm placing this code into an xml file, I'm not sure if javascript would work. I think I made a mistake.

Actually, this script is meant for a Movable Type output and I have found a plugin to write the script in Perl. Would it be easier to write the script in Perl?

4:59 pm on Oct 12, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:May 8, 2004
posts: 130
votes: 0


Thank you for all your help. But as I'm placing this code into an xml file, I'm not sure if javascript would work. I think I made a mistake.

Actually, this script is meant for a Movable Type output and I have found a plugin to write the script in Perl. Would it be easier to write the script in Perl?

5:18 pm on Oct 12, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Mar 29, 2007
posts:1147
votes: 0


That really depends on a number of things, I have no experience with Movable Type, and infact don't even know what it is.

Second, it may be easier to advise if you gave more information, without telling us what you want as the end result, we can only tell you how to extract the text you want.

You say you're outputting the xml, I assumed you were using AJAX, outputting from what? To where? When?

Is the 'Movable Type' browser/reader thing capable of JavaScript, and does it execute script downloaded after the page has loaded if it's an AJAX style.

You can use Perl, for me it would depend if the page was mainly static or if you needed a lot of processing.

5:53 pm on Oct 12, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:May 8, 2004
posts: 130
votes: 0


Ok, I'm sorry for the confusion. Here is the long story.

I have a xml file that I want to submit to google base. In the xml file, I want to have this line:

1) <g:image_link>http://www.example.com/image.jpg</g:image_link>

This is done by putting a tag <MT Entry> in the code like this:

2) <g:image_link><MT Entry></g:image_link>

where <MT Entry> = <img alt="One Particular Image" src="http://www.example.com/image.jpg" width="120" height="180" class="left"/>

So, using (2) the xml file displays:

3) <g:image_link><img alt="One Particular Image" src="http://www.example.com/image.jpg" width="120" height="180" class="left"/></g:image_link>

The question is how do I extract out the 'http://www.example.com/image.jpg' and replace the entire link like in (1) above directly by writing codes on the xml file. I think I may be able to use Perl but I'm not sure how to write the code.

2:12 pm on Oct 13, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Mar 29, 2007
posts:1147
votes: 0


I see. If you're doing this submission 'offline', i.e. not during a user browsing something then I personally would use Perl. Here's the syntax for both:

JavaScript, again replacing the double quotation marks:

var text = "<g:image_link><img ... src='http://www.example.com/image.jpg' .../></g:image_link>";
text = text.replace( "<img.*?src='(.*?)'.*?>", "$1");
alert( text);

And in Perl.....

my $text = "<g:image_link><img ... src='http://www.example.com/image.jpg' .../></g:image_link>";
$text =~ s/<img.*?src='(.*?)'.*?>/$1/;
print "$text\n";

If you're not already well versed in Perl I'll help you if you post in that forum.