locked
jQuery to grab datafld value or XML tag RRS feed

  • Question

  • User1122680954 posted

    I've got an update to make to an html template.  Basically I need to grab a couple values, do a calculation, and update a field with the result.

    The page was set up as an HTML template.  The template is grabbed, and the data that will be populated into the template is embedded into the page as XML, then the data is populated throughout the page using the "datafld" attribute of "div" elements.  Here you can see the top where the XML is embedded, and then below that I give you an example of how the data is populated into the page.

    <body>
        <xml id="XmlData">
            <?xml version='1.0' ?>
    
            <Header>
                <subheader>
                    <ItemCode>Item Code</ItemCode>
                    <description>Description of Item</description>
                    <quantity>3</quantity>
                    ...
    
    
    ...
    <table border="0" width="100%" cellspacing="3" cellpadding="1" datasrc="#XmlData">
         <tr>
             <td width="30%" align="left" class="Detail">Quantity:&nbsp;</td>
             <td><div id="qtyID" datafld="quantity"></div></td>

    The problem is, when I grab the div where the datafld is being used, it simply grabs the datafld attribute (ex above, "quantity").  I can't grab the actual value "3".  So, my question is, is there a good way to grab the actual value that the datafld is referencing using jQuery?  Or, is there a good way to grab the tag value from the embedded XML section directly, using jQuery? I can't change anything about how the XML gets embedded (such as adding attributes to the tags or anything).

    The closest I have come is using the code below, which grabs the datafld property, meaning it returns "quantity".

    var Quantity = $("#qtyID").attr("datafld");

    Friday, March 9, 2018 7:07 PM

Answers

  • User-832373396 posted

    Hi lewis886,

    .  I can't grab the actual value "3".  So, my question is, is there a good way to grab the actual value that the datafld is referencing using jQuery? 

    Sir, In jquery, please refer to this simple example

    var xml = "<rss version='2.0'><channel><title>RSS Title</title></channel></rss>",
      xmlDoc = $.parseXML( xml ),
      $xml = $( xmlDoc ),
      $title = $xml.find( "title" );
      alert( $title.text() ); 

    Result 

    alert RSS Title 

    For one xml object, $xml.find( "title" )

    https://api.jquery.com/jQuery.parseXML/ 

    For array xml, $(xml).find('Book').each( 

    http://www.jquerybyexample.net/2012/04/read-and-process-xml-using-jquery-ajax.html   

    and if your xml string is in a div please use this way to get the xml string from a div

    $('div').html()

    Welcome to back if met any question :)

    With regards, Angelina Jolie

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 12, 2018 9:23 AM

All replies

  • User-474980206 posted

    the xml will loaded as plain dom elements (of unknown types).

       var q = $('#XmlData Header subheader quantity').text();

    I would have expected the datafld attribute to look like an xpath, say: "/Header/subheader/quantity", so you know how to access (not having to know the root path). in which case a simple mapping would have worked:

      var $xml = $('#XmlData');
    
      // get xml value
      var path = $("#qtyID").attr("datafld").split('/').join(' ');
      var value = $xml.find(path);
    
      // update
      $('#qtyid').text(value);
    

    Sunday, March 11, 2018 10:04 PM
  • User-832373396 posted

    Hi lewis886,

    .  I can't grab the actual value "3".  So, my question is, is there a good way to grab the actual value that the datafld is referencing using jQuery? 

    Sir, In jquery, please refer to this simple example

    var xml = "<rss version='2.0'><channel><title>RSS Title</title></channel></rss>",
      xmlDoc = $.parseXML( xml ),
      $xml = $( xmlDoc ),
      $title = $xml.find( "title" );
      alert( $title.text() ); 

    Result 

    alert RSS Title 

    For one xml object, $xml.find( "title" )

    https://api.jquery.com/jQuery.parseXML/ 

    For array xml, $(xml).find('Book').each( 

    http://www.jquerybyexample.net/2012/04/read-and-process-xml-using-jquery-ajax.html   

    and if your xml string is in a div please use this way to get the xml string from a div

    $('div').html()

    Welcome to back if met any question :)

    With regards, Angelina Jolie

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 12, 2018 9:23 AM
  • User1122680954 posted

    It ended up being a little tougher, but you basically had the answer.  I ended up having to strip out line breaks and other characters that got introduced from the embedding, and once I did that I didn't even have to parse it out as XML.

    var text = $('#XmlData');
    var text2 = text[0].innerHTML;
    var xml = text2.replace(/[\r\n]/g, '');
    var xml2 = xml.replace(/\s/g, '');
    
    $xml = $(xml2),
    $quantity = $xml.find("quantity");
    var QuantityNum = parseInt($quantity.text());

    Thanks a lot.  :)

    Monday, March 12, 2018 8:18 PM