最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript - parse XML data - Stack Overflow

programmeradmin3浏览0评论

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

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

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

Share Improve this question edited Apr 26, 2017 at 11:47 reddragon72 asked Apr 25, 2017 at 20:14 reddragon72reddragon72 1911 gold badge3 silver badges17 bronze badges 4
  • You have to iterate through the children – Mehrad 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". – Heretic Monkey 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. – guest271314 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. – reddragon72 Commented Apr 26, 2017 at 11:46
Add a ment  | 

2 Answers 2

Reset to default 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 (`).

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./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
})
发布评论

评论列表(0)

  1. 暂无评论