0

I've been browsing through stackoverflow for a while and have not found an answer that suits my problem - I want to load in a certain part of my xml file when I click a button. I want to load parts of my xml file into 'contents' div.

This is my HTML Code :

<div>
  <input type="button" id="click"/>
  <div id="content">

  </div>
</div>

This is my XML :

<?xml version="1.0" encoding="utf-8"?>
<books>
    <book id="jeff">
    <author>Jeffrey </author>
    <title>Books 1</title>
</book>
<book id="jerry">
    <author>Jerry</author>
    <title>Books 2</title>
</book>
<book id="jimmy">
    <author>Jimmy</author>
    <title>Boks 3</title>
</book>
<book id="jem">
    <author>Jem</author>
     <title>Books 4</title>
</book>
</books>

I have tried this (I know this is far from right - so no hate please) :

$(document).ready(function() {
 $('#click').click(function(){
    $.ajax({
      url : 'books.xml',
      dataType : 'xml',
    success : parse,
     });

   function parse(xml){
     $('#content').append($(xml).find('authour').text());
       }
   });
 });

How would I access each authours id specification using jQuery. (Sorry if I messed up explaining it ! :) ).

3
  • stackoverflow.com/questions/1773550/… - this might help you. Change your xml to json and then iterate through it. Commented Feb 23, 2016 at 14:43
  • @MaciejKwas no need to convert to json just to read xml and pars to html Commented Feb 23, 2016 at 14:45
  • So what have you tried. Can you load the xml document and store it into a variable? Can you loop over the document? Commented Feb 23, 2016 at 15:00

1 Answer 1

1

It is possible to load the xml using jQuery with $.ajax, $.get, $.post. Once you are using jQuery it has a method $.parseXML:

$.ajax({url: 'file.xml', method: 'GET', dataType: 'xml'}).done(function (xml) {
  var xmlDoc = $.parseXML( xml );
  var author = $(xmlDoc).find('book[id="jem"]').find('author');
  $('#content').append('<span>' + author.text() + '</span>');
});

The server should respond with the correct mime type Content-type: "text/xml" in this case

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

4 Comments

how would I output that into the content div and how would I select the author of a specific id such as jeff, jerry jimmy etc
You might want to create some html to append it where you want, I'll edit
Replaced file.xml with books.xm - l(which is the name of the actual file) but still returns nothing , thanks for the help anyways.
The url must be the either absolute or relative. If is relative then it will use the domain and whole path of the actual page and append a '/' and he url that you provided. This is the way i made the example. If you add the '/' to the url then it will use the domain of the page and the url that you provide. Other possibility is to pass an absolute url.

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.