Vap0r
Vap0r

Reputation: 2616

Parsing XML with jQuery

I'm having trouble parsing this xml file using jquery:

<?xml version="1.0" encoding="utf-8"?>
<?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:BOT-Memos:-myXSD-2011-07-13T14-29-57" solutionVersion="1.0.0.27" productVersion="14.0.0.0" PIVersion="1.0.0.0" href="http://sharept03sb1/BOT/BOT%20Memos/Forms/template.xsn"?>
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.3"?>
<my:myFields xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:pc="http://schemas.microsoft.com/office/infopath/2007/PartnerControls" xmlns:ma="http://schemas.microsoft.com/office/2009/metadata/properties/metaAttributes" xmlns:d="http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields" xmlns:q="http://schemas.microsoft.com/office/infopath/2009/WSSList/queryFields" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:dms="http://schemas.microsoft.com/office/2009/documentManagement/types" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2011-07-13T14:29:57" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-US">
    <my:content>
        <my:title>Hello</my:title>
        <my:description><html xmlns="http://www.w3.org/1999/xhtml" xml:space="preserve"><div>How are you?</div></html></my:description>
        <my:memo xsi:nil="true"></my:memo>
        <my:group>

        <pc:Person xmlns:pc="http://schemas.microsoft.com/office/infopath/2007/PartnerControls"><pc:DisplayName>Johnny Doe</pc:DisplayName><pc:AccountId>ADMIN\fakeadminuser</pc:AccountId><pc:AccountType>User</pc:AccountType></pc:Person></my:group>
        <my:meetingDate></my:meetingDate>
        <my:addAttachments>
            <my:attachments>
                <my:attachment xsi:nil="true"></my:attachment>
            </my:attachments>
        </my:addAttachments>
        <my:upload></my:upload>
    </my:content>
</my:myFields>

I'm trying to read the my:title node, but that I'm having no luck. This is the particular line I'm using to parse it.

$.get('target.xml', function(data){
  alert($(data).find("my:memo").text());
});

Any ideas?

Upvotes: 0

Views: 893

Answers (3)

Justin ᚅᚔᚈᚄᚒᚔ
Justin ᚅᚔᚈᚄᚒᚔ

Reputation: 15359

jQuery may not be recognizing the data as XML for some reason. Also, namespaced tags are usually better selected by explicitly checking the nodeName. Try this:

$.get('target.xml', function(data) {
    alert($(data).find("[nodeName=my:memo]").text());
}, "xml");

Or:

$.ajax({
    type: "GET",
    url: "target.xml",
    dataType: "xml",
    success: function(data) {
        alert($(data).find("[nodeName=my:memo]").text());
    }
});

Upvotes: 1

switz
switz

Reputation: 25228

You're looking for a tag, but the tag has this in it: my:memo xsi:nil="true"

$.get('target.xml', function(data){
  alert($(data).find("my:memo xsi:nil=\"true\"").text());
});

There are better ways to do this, I believe.

Upvotes: 0

Mrchief
Mrchief

Reputation: 76258

Guess you need to escape the colon: alert($(data).find("my\\:memo").text());

This one has a good discussion: jQuery XML parsing with namespaces on it

Upvotes: 0

Related Questions