Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8102567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:16:25+00:00 2026-06-05T23:16:25+00:00

I am currently pulling an XML feed into a webapp through ajax. Obviously i

  • 0

I am currently pulling an XML feed into a webapp through ajax. Obviously i like using jquery to parse it, like so:

$(source).find("item"); 

The problem with this is that everything is converted into nodes, including -tags. Considering this is some sort of RSS feed, and the feed contains complete articles (including image galleries), there are many img-tags. To prevent this, i would like to try and convert the img-tags to something like this:

before:

<img src="path_to_img.jpg" width="450" height="199" alt="alt description" title="image title" class="image_classes" />

after:

<image>
<src>path_to_img.jpg</src>
<alt>alt description</alt>
<title>image title</title>
<class>image_classes</class>
</image>

If anyone has better suggestions than using regexes, those are of course welcome too. But because it all has to be treated like text, I fear there are few, considering images start preloading when they are added to the DOM.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T23:16:26+00:00Added an answer on June 5, 2026 at 11:16 pm

    Well, it’s not going to be very easy unless you can guarantee all those attributes will always be present, and always in quoted strings etc etc.

    var html = '<img src="path_to_img.jpg" width="450" height="199" alt="alt description" title="image title" class="image_classes" />';
    var regex = /<img .*?(src|alt|title|class)="([^"]*)" .*?(src|alt|title|class)="([^"]*)" .*?(src|alt|title|class)="([^"]*)" .*?(src|alt|title|class)="([^"]*)".*?\/?>/g;
    var xmlTemplate = '<image><$1>$2</$1><$3>$4</$3><$5>$6</$5><$7>$8</$7></image>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​';
    var xml = html.replace(regex, xmlTemplate);
    

    If your source HTML is not as uniform, then you might have to use .replace(regex, func) instead of .replace(regex, string).

    As you have said the alt attribute may not be present, then you will need to use regex with a function, like this:

    var html = '<img src="path_to_img.jpg" width="450" height="199" alt="alt description" title="image title" class="image_classes" />';
    var regex = /<img .*?>/gi;
    function getAttributeValue(tag, attribute)
    {
        var regex = new RegExp('\\b' + attribute + '="([^"]*)"', 'i');
        var match = tag.match(regex);
        return '\t<' + attribute + '>' + (match ? match[1] : '') + '</' + attribute + '>\n';
    }
    
    var xml = html.replace(regex, function($0)
    {
        var xml = '<image>\n';
        xml += getAttributeValue($0, 'src');
        xml += getAttributeValue($0, 'alt');
        xml += getAttributeValue($0, 'title');
        xml += getAttributeValue($0, 'class');
        xml += '</image>';
        return xml;
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm pulling data from an XML feed and manipulating it using the awesome SimpleXMLElement.
I have this find condition pulling from my model that currently looks like this.
I am using SAX to parse an XML file I'm pulling from the web.
I am currently pulling start times from an XML file and using a date
I'm currently pulling emails from an exchange inbox like so... var exchangeService = new
I currently am pulling menu data out of our database using the PDO fetchAll()
Currently developing an application using the newest version of symfony, obtained through PEAR. This
Currently I'm using <pre><code> code here </code><pre> to display code. I'm pulling this information
We're currently pulling jQuery and jQueryUI (and jQueryUI CSS) libraries from the google CDN.
I am currently pulling an RSS feed in which the the format is essentially

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.