28 Oct

Parsing XML from JQuery

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>

<p id="someElement"></p>
<p id="anotherElement"></p>

<script>
var xml = "<rss version='2.0'><channel><title>RSS Title</title></channel></rss>",
    xmlDoc = $.parseXML( xml ),
    $xml = $( xmlDoc ),
    $title = $xml.find( "title" );

/* append "RSS Title" to #someElement */
$( "#someElement" ).append( $title.text() );

/* change the title to "XML Title" */
$title.text( "XML Title" );

/* append "XML Title" to #anotherElement */
$( "#anotherElement" ).append( $title.text() );
</script>

</body>
</html>

Keterangan:
Baris 4: isi dengan path jquery sesuai dengan kebutuhan
Baris 12: contoh isi XML yang akan di parsing
Baris 13: parsing XML document dari JQuery
Baris 15: contoh pencarian element tertentu dalam XML (title)

Leave a Reply

Your email address will not be published. Required fields are marked *