简体   繁体   English

JavaScript-解析XML数据

[英]JavaScript - parse XML data

All of the other posts utilize parsing a simple XML and I need to see how to parse sub levels. 所有其他文章都利用解析简单的XML,我需要了解如何解析子级别。

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. 在Powershell中,您可以按(book.bookid.booktitle)进行挖掘,但是我在Javascript中看不到。 Another thing is that the id's , remain the same for each book but I need the name of each book. 另一件事是,每本书的ID保持不变,但我需要每本书的名称。

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. 每本书我都需要Cookbook> bookid> booktitle>。 I have tried setting the values for the node and child node but it never shows a returned value just blank or null. 我尝试设置节点和子节点的值,但它从未显示返回值只是空白或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 这个站点的信息很好,但是又是一个级别... 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. 我正在解析的xml在其他嵌套组下的其他位置列出了书名,例如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. 我想要此组Cookbook> bookid> booktitle>,因为不需要其他标题,因此搜索书名将同时返回cook和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. 您可以在浏览器中使用jQuery,或在NodeJS中使用cheerio(为服务器构建的jQuery的子集, https: //www.npmjs.com/package/cheerio)轻松解析XML。 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. 学习该API可能需要花费一些时间来继承XML文档和/或遍历元素,但是它非常简单易用。

// 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 (`). Javascript不喜欢多行变量,要么将整个字符串放在一行上,要么使用反斜杠(`)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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