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

  • Home
  • SEARCH
  • 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 322005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:53:52+00:00 2026-05-12T08:53:52+00:00

I am trying to create a sort of generic xml parser, like this: Part

  • 0

I am trying to create a sort of generic xml parser, like this:

Part 1:

An object with filter is created:

var product = {

  holder : {
    id: '',
    title: '',
    text : '',
    price: ''
  },

  filter : {
    id: '',
    test: function( elementHolder ) {
      if( elementHolder.id == product.filter.id ) {
        return true;
      }
      return false;
    }
  }
}

Part 2:

The xml parser:

/*
 * Generic XML parser
 * Parses the url into elementHolder objects
 */
var xmlParser = {

  parseXml: function( url, node, element, functie ) {

    var i = 0;       // result counter

    var result = []; // the result array
    /*
     * Open the xml file
     */
    jQuery.get( url, function( data ){

      /*
       * Loop through the results, if we have a filter, apply it.
       */
      jQuery( data ).find( node ).each( function(){
        /*
         * Copy the element holder
         */
        var elementHolder = element.holder;
        var elementData = jQuery(this);

        /*
         * Fill the copied holder with the xml data
         */
        elementData.children().each(function() {
          elementHolder[ this.nodeName ] = jQuery(this).text();
        });

        /*
         * if the filter applies, add the holder to the result
         */
        if( element.filter.test( elementHolder ) ) {
          result[i] = elementHolder; $i++;
        }           

        // console.log( result );

      });

      /*
       * If there is a callback ( prevents the result from parsing before it is ready )
       */
      try {
        if (typeof functie == "undefined") {
          throw 'There is no callback function specified.';
        }
      } catch( e ) { alert( e ); return; }

      functie( result );

    });    

  }

}

Part 3:

Calling on document ready:

product.filter.id = 11;

  /*
   * parsen
   */
  var productXml = 'test.xml';
  xmlParser.parseXml( productXml, "product", product, function( data ) {
    console.log( 'result:' );
    console.log( data );
  });

Part 4:

The test.xml:

<data>
  <products>
    <product>
      <id>1</id>
      <title>test 1</title>
      <text>text 1</text>
      <price>1.00</price>
    </product>
    <product>
      <id>2</id>
      <title>test 2</title>
      <text>text 2</text>
      <price>2.00</price>
    </product>
    <product>
      <id>3</id>
      <title>test 3</title>
      <text>text 3</text>
      <price>3.00</price>
    </product>
  </products>
</data>

It all works fine except for one thing;

The result get overwritten each loop by the newly created holder, does anyone have any ideas how to fix this? ( i think it is does have something to do with the scope ).

Thanks !

  • 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-05-12T08:53:53+00:00Added an answer on May 12, 2026 at 8:53 am

    I found the solution

    The part:

    var elementHolder = element.holder;
    

    Doesnt create a copy of the object, just another reference;

    I found the solution to that on this page:

    http://my.opera.com/GreyWyvern/blog/show.dml/1725165

    Object.prototype.clone = function() {
      var newObj = (this instanceof Array) ? [] : {};
      for (i in this) {
        if (i == 'clone') continue;
        if (this[i] && typeof this[i] == "object") {
          newObj[i] = this[i].clone();
        } else newObj[i] = this[i]
      } return newObj;
    };
    
    var elementHolder = element.holder.clone();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 231k
  • Answers 231k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer you first would need to format string and then use… May 13, 2026 at 2:06 am
  • Editorial Team
    Editorial Team added an answer Just use the algorithm as shown on wikipedia, understand it… May 13, 2026 at 2:06 am
  • Editorial Team
    Editorial Team added an answer You need a coalesce in there. It selects the first… May 13, 2026 at 2:06 am

Related Questions

I want to setup a statistics monitoring platform to watch a specific service, but
I am trying to use Python's ElementTree to generate an XHTML file. However, the
Using .NET (if I have to use pInvoke or managed C++, that's fine too),
I am trying to create a new website on a remote server via msbuild

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.