homepage Welcome to WebmasterWorld Guest from 54.227.160.102
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Using javascript instead of regex
on .xml file
spica42

10+ Year Member



 
Msg#: 3474575 posted 6:22 am on Oct 11, 2007 (gmt 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!

 

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 3474575 posted 2:39 pm on Oct 11, 2007 (gmt 0)

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.

Dabrowski

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3474575 posted 2:45 pm on Oct 11, 2007 (gmt 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.

Dabrowski

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3474575 posted 2:46 pm on Oct 11, 2007 (gmt 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.

Dabrowski

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3474575 posted 3:47 pm on Oct 12, 2007 (gmt 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);

spica42

10+ Year Member



 
Msg#: 3474575 posted 4:59 pm on Oct 12, 2007 (gmt 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?

spica42

10+ Year Member



 
Msg#: 3474575 posted 4:59 pm on Oct 12, 2007 (gmt 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?

Dabrowski

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3474575 posted 5:18 pm on Oct 12, 2007 (gmt 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.

spica42

10+ Year Member



 
Msg#: 3474575 posted 5:53 pm on Oct 12, 2007 (gmt 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.

Dabrowski

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3474575 posted 2:12 pm on Oct 13, 2007 (gmt 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved