2

All of the other posts utilize parsing a simple XML and I need to see how to parse sub levels.

What others post...

<book>
 <booktitle>something</booktitle>
 <author>someone</author>
</book>

easy enough... but this is what I am dealing with and I need to start at cookbook...

<cookbook>
 <bookid>
   <booktitle>something</booktitle>
   <author>someone</author>
 </bookid>
 <bookid>
   <booktitle>something</booktitle>
   <author>someone</author>
 </bookid>
</cookbook>

In Powershell you can dig down by (book.bookid.booktitle) but I am not seeing this in Javascript. Another thing is that the id's , remain the same for each book but I need the name of each book.

var parser = new DOMParser();
var xmlDoc = parser.parseFromString(book, "text/xml");
var first = xmlDoc.getElementsByTagName("cookbook")[0].childNodes[0].nodeValue;

I need the cookbook>bookid>booktitle> for each book. I have tried setting the values for the node and child node but it never shows a returned value just blank or null. Again all the posts I have sen on here deal with one level not three deep and that is what is throwing me off.

This site had good info but again one level... https://developer.mozilla.org/en-US/docs/Web/Guide/Parsing_and_serializing_XML

Let me be clear on something. The xml I am parsing has the booktitle listed in other locations under other nested groups, say dogbook>bookid>booktitle. I want this group cookbook>bookid>booktitle> as the other titles are not wanted so searching for booktitle will return both cook and dog. Forgot that major important part duh...

4
  • You have to iterate through the children Commented Apr 25, 2017 at 20:18
  • What exactly do you need to know about the node? In other words, if you just need the value of all the booktitle nodes, you can use XPath via document.evaluate with something like "//booktitle". Commented Apr 25, 2017 at 20:22
  • "Another thing is that the id's , remain the same for each book" id of element in document should be unique. Commented Apr 25, 2017 at 20:24
  • I need the booktitles but only for the cookbooks so I cannot just iterate through booktitle as that will give me all titles in all categories. As for iterate I adjusted the ("cookbook")**[0]**childNodes**[0]**.nodeValue numbers and nothing was returned. I also adjusted my question to show the correct level I am trying to search under. It is not booktitle but cookbook. Commented Apr 26, 2017 at 11:46

2 Answers 2

3
var book = `<cookbook>
 <bookid>
   <booktitle>something delicious</booktitle>
   <author>someone</author>
 </bookid>
 <bookid>
   <booktitle>something else delicious</booktitle>
   <author>someone</author>
 </bookid>
</cookbook>`;

var parser = new DOMParser();
var xmlDoc = parser.parseFromString(book, "text/xml");
var first = xmlDoc.getElementsByTagName("booktitle")[0].childNodes[0].nodeValue;

console.log(first);

This prints 'something delicious' to the console. Javascript doesn't like multi-line variables either put the entire string on one line or use back tick (`).

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

1 Comment

I have this but it also gives me all the books in all other categories. I need to stay withing the cookbook category as there are booktitles everywhere listed under dogbook and dyibook.
3

EDIT to illustrate looping through books below:

You can use jQuery in the browser, or cheerio (a subset of jQuery built for the server, https://www.npmjs.com/package/cheerio) in NodeJS to parse XML easily. It might take a little time to learn the API to descend the XML doc and/or loop through elements, but it's pretty straightforward and easy to use.

// if jQuery or cheerio is bound as the `$` variable

const myXml = `<books>
  <cookbook>
    <bookid>
      <booktitle>My Cookbook 1</booktitle>
      <author>someone1</author>
    </bookid>
    <bookid>
      <booktitle>My Cookbook 2</booktitle>
      <author>someone</author>
    </bookid>
    <bookid>
      <booktitle>My Cookbook 3</booktitle>
      <author>someone</author>
    </bookid>
  </cookbook>
  <dogbook>
    <bookid>
      <booktitle>My Dogbook 1</booktitle>
      <author>someone</author>
    </bookid>
    <bookid>
      <booktitle>My Dogbook 2</booktitle>
      <author>someone</author>
    </bookid>
  </dogbook>
</books>`

const $myXml = $( $.parseXML(myXml) )
$firstCookBook = $myXml.find('cookbook').find('bookid').first()

$firstCookBook.children('booktitle').text()
// 'My Cookbook 1'

$firstCookBook.children('author').text()
// 'someone1'


// looping through all cookbook titles at books > cookbook > bookid > booktitle
$myXml.children('books').children('cookbook').each(function(index) {
  console.log($( this ).find('booktitle').text())
  // My Cookbook 1
  // My Cookbook 2
  // My Cookbook 3
})

6 Comments

Yours looks the closest. I adjusted my question to show the level I am looking at as I need to start at the cookbook level and iterate through each bookid and then the booktitle. There are other listings in the xml such as dogbooks, dyibooks and so forth but I do not want those so I need to start at the cookbook level to be able to segregate the kind of book.
I updated my answer to include both cookbooks and dogbooks, and showed how to only loop through the cookbooks. If this still doesn't illustrate how you could accomplish what you need, if you can provide a specific example of the XML you want to parse, I can update my answer with how to get the booktitles for it.
Hmmm I had to adjust your xml as I kept getting an error with it. After that the code worked as is, but when I added in my xml from my GET source it did not work, I am getting a blank. I can see the XML as it sits in a text box and can print out all the booktitles from my code posted above but your code is apparently not picking up my XML from a GET. I am digging through it now to take a look.
Again, if you provide the exact XML you need to parse I could help adjust the jQuery above to make it work, but I don't know what it looks like exactly. Also, I copied and pasted the code I have above directly into a browser console that has jQuery on the page, and it works fine. What error are you getting? If you need to call this code after you get XML from an AJAX request, you probably should wrap the above code in a function and call it after you've gotten your XML. It's probably just an asynchronous issue is my guess..?
Ok New stuff. I took your code and a slight mod.. $myXml.find('cookbook').each(function(index) { console.log($( this ).find('booktitle').text()) and this is returning all the titles in one line. I am trying to see how to sort this now to add a comma between each book title. Thanks for this help!! this really got me where I needed to go. Odd though that I could not parse this any other way....
|

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.