1

I am trying to parse the xml but its not returning anything

here is the code jquery and xml code below. Is there something i am doing wrong?

<script language="javascript">

   /* $(document).ready(function()
    {
      $.ajax({
        type: "GET",
        url: "view_xml.xml",
        dataType: "xml",
        success: function(xml) { parseXml(xml); }
      });
    });
    */
$(document).ready(function()
{
  $.ajax({
    type: "GET",
    url: "view_xml.xml",
    dataType: "xml",
    complete: function(data,status) { parseXml(data.responseXML); }
  });
});

function parseXml(xml)
{
  $(xml).find("inst:cView").each(function()
  {
    $("#output").append($(this).attr("type") + "<br />");
    /* output
    Disks
    Disks

    */
  });

 $(xml).find("inst:field").each(function()
  {
    $("#output").append($(this).attr("name") + "-");
    $("#output").append(": " + $(this).find("name").text() + "<br />");
    /* output
     TargetObjectClass -:Disk
     TargetObjectName -:DISK A1
     DisplayName -:DISK-Name
     MaxAvgDataRate KB/sec -:50.00 KB/sec
     MaxAvgQueueDepth -:50.00 
     ...
     ...

    */
  });
}
</script>

XML code

<?xml version="1.0" encoding="utf-8"?>
    <entry>
    ----
    ----
    <cView type="D1">
                    <field name="TargetObjectClass">Disk</field>
                    <field name="TargetObjectName">DISK A1</field>
                    <field name="DisplayName">DISK-Name</field>
                    <field name="MaxAvgDataRate KB/sec">50.00 KB/sec</field>
                    <field name="MaxAvgQueueDepth">50.00</field>
                </cView>
                <cView type="D2">
                    <field name="TargetObjectClass">Disk</field>
                    <field name="TargetObjectName">DISK B2</field>
                    <field name="DisplayName"> Disk-Name 2 </field>
                    <field name="MaxAvgDataRate KB/sec">60.00 KB/sec</field>
                    <field name="MaxAvgQueueDepth">60.00</field>
                </cView>


    ...
    </entry>
3
  • Not sure, but have you tried using 'xml' instead '$(xml)' ? Commented Feb 18, 2012 at 18:39
  • tried that didnt output anything Commented Feb 18, 2012 at 18:43
  • yes it valid xml i had left it out here Commented Feb 18, 2012 at 19:04

1 Answer 1

1

Try following

$(document).ready(function()
{
  $.ajax({
    type: "GET",
    url: "view_xml.xml",
    dataType: "xml",
    complete: function(data,status) { parseXml(data.responseText); }
  });
});

Edit regarding your xml tags :

Using data.responseText
And <cView type="..."> instead of <inst:Cview type="..."> works like a charm.
The same for <field ...> instead <inst:field ...>.

Because ":" makes the selector search some "pseudo-element" and not a tag.

So, finally, you just have to escape ':', like that :

$(xml).find("inst\\:cView")...
$(xml).find("inst\\:field")...

Edit regarding your new XML

To get the arborescence of data, use following :

function parseXml(xml)
{
  $(xml).find("cView").each(function()
  {
    $("#output").append($(this).attr("type") + "<br/>"); 
    $(xml).find("field").each(function()
    {
        $("#output").append($(this).attr("name") + " : ");
        $("#output").append($(this).text() + "<br/>");
    });
    $("#output").append("<hr/>"); 
  });
}

The content of each "field" is $(this).text();

Sign up to request clarification or add additional context in comments.

2 Comments

I think that I've found the problem, please check my answer if it's right :-)
Any suggestion how i can retieve the inside value for example <field name="MaxAvgQueueDepth">60.00</field> i cant seem to get the 60.00 out?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.