29092

Parsing complex XML with jQuery

Question:

I'm trying to parse the following XML with the following jQuery. Nothing is displayed on the page. I know it's getting the file. I've looked online and though there are many examples of parsing xml with jquery none of them looked like this formatting.

XML:

<?xml version="1.0" encoding="utf-8"?> <wcm:root xmlns:wcm="http://www.stellant.com/wcm-data/ns/8.0.0" version="8.0.0.0"> <wcm:element name="title"></wcm:element> <wcm:element name="wide_image">&lt;img src=&#39;[!--$wcmUrl&amp;amp;x28;&#39;resource&amp;#39;,&amp;#39;CMS3_130980&amp;#39;&amp;amp;x29;--]&amp;#39;/&gt;</wcm:element> <wcm:element name="image">&lt;img src="[!--$wcmUrl('resource','CMS3_132821')--]"/&gt;</wcm:element> <wcm:element name="body">&lt;p&gt; Paragraph of text goes here.&lt;br /&gt;&lt;br /&gt; Paragraph of text goes here.&lt;br /&gt;&lt;br /&gt; Paragraph of text goes here.&lt;br /&gt;&lt;br /&gt; Paragraph of text goes here.&lt;br /&gt;&lt;br /&gt; Paragraph of text goes here.&lt;/p&gt; </wcm:element> </wcm:root>

HTML & jQuery:

<html> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function () { $.ajax({ type: "GET", url: "link_to_my_file.xml", dataType: "xml", success: parseXml }); }); function parseXml(xml){ $(xml).find("wcm\\3a root").each(function(){ alert("Test"); $("#output").append($(this).attr("title") + "<br />"); $("#output").append($(this).attr("wide_image") + "<br />"); $("#output").append($(this).attr("image") + "<br />"); $("#output").append($(this).attr("body") + "<br />"); }); }; </script> <div id="output"></div> </html>

Answer1:

You have to <a href="http://mathiasbynens.be/notes/css-escapes" rel="nofollow">escape</a> the : in the selector: <a href="http://mothereff.in/css-escapes#1wcm%3Aroot" rel="nofollow">http://mothereff.in/css-escapes#1wcm%3Aroot</a>

So, instead of:

$(xml).find("wcm:root")

Use:

$(xml).find("wcm\\3a root")

Also, your code snippet is invalid JavaScript (missing } after the parseXml function declaration). Check your error console.

Recommend

  • Deleting files in a server using shell script
  • getting rejected non fast forward issue in egit
  • “gift” an In-App-Purchase Android
  • section attribute of a function template is silently ignored in GCC
  • how to count horizontal values on a database?
  • How to refresh a single row in a table?
  • Does a zero change jump on x86 clear the instruction prefetch queue?
  • Create custom FrameworkContentElement to add diagonal line over text in WPF
  • Haskell type length + 1
  • Redirect stdin from a file in eclipse with run configuration
  • C: How to delete adjacent duplicates in 1D array?
  • Running Rounding
  • Spark SQL slow execution with resource idle
  • Lazy loading in datatable JSF
  • Java Hibernate Check MySQL Replication is in sync
  • How to filter out rows from spark dataframe containing unreadable characters
  • svnserve and SASL problem
  • how to synchronize web site content while loading page
  • Firebase suddenly reports invalid signature
  • Hyperlink to Outlook Attachment
  • Python Asyncio run_forever() and Tasks
  • Using XPATH to isolate inline javascript
  • Using django-multiupload within a ModelForm
  • Remove stopwords and tolower function slow on a Corpus in R
  • Slick: How can I combine a SQL LIKE statement with a SQL IN statement
  • Auto send email based on the time and email address in database
  • Why do you need 2 Javascript files for cross-platform Cordova plugin?
  • Wireshark Display Filter for Unique Source/Destination IP and Protocol
  • ASP.NET MVC 2 actions for the same route?
  • Ember.js + JQuery-UI Tooltip - Tooltip does not reflect the model / controller changes
  • Year over Year Stats from a Crossfilter Dataset
  • Ruby regex for matching simpliest Ruby's regexes
  • How to split wav file into two or more parts using c#
  • Content-Type alternative in MQTT
  • Why is ordered choice in pyparsing failing for my use case?
  • How to clear a browser cache in Protractor
  • Google App Engine Datastore: Dealing with eventual consistency
  • How to encrypt Connectionstring written in web.config from codebehind?
  • Call Microservice from another Microservice within Docker
  • Cross compile glibc for arm, got undefined reference to some unwind functions