Question

I am trying to render my xml using the jqGrid 4.6 and jquery 1.11. I am having trouble reading the attributes with namespaces.

Below is my xml:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
 <feed>
   <id>1234/</id>
   <title type="text">ABCD</title>

   <entry>
     <id>1</id>
     <title type="text">Entry 1</title>
      <author>
       <name />
      </author>
     <content type="application/xml">
      <m:properties>
       <d:ContentTypeID>1</d:ContentTypeID>
       <d:TitleOfItem />
       <d:lastupdated>2014-04-22</d:lastupdated>
      </m:properties>
     </content>
  </entry>
  <entry>
      <id>2</id>
      <title type="text">Entry 2</title>
      <author>
        <name />
      </author>
      <content type="application/xml">
        <m:properties>
        <d:ContentTypeID>2</d:ContentTypeID>
        <d:TitleOfItem />
        <d:lastupdated>2014-04-22</d:lastupdated>
        </m:properties>
      </content>
   </entry>
</feed>

script for the Grid:

<script type="text/javascript">

jQuery(document).ready(function(){

$("#grid")
        .jqGrid({
          url:'mytemplate.xml',
          datatype: "xml",
          xmlReader: {
                root:"feed",
                row:"entry",
                repeatitems:false
          },
          colModel:[
                      {name:'id', index:'id', width:100, align:'center', xmlmap: "id"},
                      {name:'ContentTypeID', index:'invid', width:100, align:'center', xmlmap:"????"}
                    ],
            height:250,
            rowNum:10,
            rowList:[10,20,30],
            viewrecords: true,
            loadonce: true
});

});


</script>

How do I read the ContentTypeID from the above xml? I tried the function

xmlmap: function (obj) {
            return $(obj).find(">content>RequestedDeliveryDetails").attr("ContentTypeID");
        }

and also the

xmlmap: "[nodeName=content.m:properties.d:ContentTypeID]" },

no luck so far.

Thanks in advance!

Was it helpful?

Solution

I found the solution, need to escape the ':' while specifying the namespaced node name

xmlmap: "ns\\:nodeName" }

so for my examle xml above

colModel:[
  {name:'id', index:'id', width:100, align:'center', xmlmap: "id"},
  {name:'ContentTypeID', index:'invid', width:100, align:'center', xmlmap:"d\\:ContentTypeID"}
 ]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top