简体   繁体   中英

JavaScript - parse XML data

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...

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
})
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 (`).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM