0

I have the following xml with special character ":" in the object header. How to parse it.

<p606:ResponseData xmlns:p353="">
<p353:PersonId idOwner="loA">
<p353:IdValue name="78N">1fgsth</p353:IdValue> 
</p353:PersonId>
<p353:CInfo effectiveDate="2010-03-13" xsi:type="p353:cnt">
<p353:TCode>OYYT</p353:TCode> 
<p353:Ext>100</p353:Ext> 
</p353:ContactInfo>
</p606:ResponseData>

I used the following

xmlDoc = $.parseXML( xml ),
$xml = $( xmlDoc ),
$title = $xml.find( "p353:TCode" );
$( "#somePlace" ).append( $title.text() );

But it didn't work...moreover i want the value of effectiveDate from the follwoing line

<p353:CInfo effectiveDate="2010-03-13" xsi:type="p353:cnt">

How to do it

user1853803
  • 649
  • 3
  • 8
  • 27

2 Answers2

1

First of all you should escape the : character, it means pseudo class by default, in jQuery selectors.

$xml = $(xml);
Try this for title: $xml.find("p353\\:TCode").text();
And for date: $xml.find("p353\\:CInfo").attr('effectiveDate');

And the following question may helps you: jQuery XML parsing with namespaces

Community
  • 1
  • 1
Kovge
  • 2,019
  • 1
  • 14
  • 13
  • none of them are working console.log($("p353\\:Ext").text()); console.log($(xml).find('[nodeName="p353:Ext"]')); – user1853803 Mar 04 '13 at 10:41
  • 1
    Oh man .. You are right... $ should be $xml.find in my example, i fixed it. And $xml = $(xml); – Kovge Mar 05 '13 at 09:04
1

Those are prefixes which refer to namespace URIs. You need a naespace-aware parser. Please note that the name of the node is ResponseData not p606:ResponseData.

The prefixes should be declared somewhere in the parent chain of the element like:

<p606:foo xmlns:p606="http://www.example.com/portal/606">
    <p606:bar property="value"/>
</p606:foo>

In the fragment it is clear that p353 is not defined.

vbence
  • 20,084
  • 9
  • 69
  • 118