如何通过id搜索book并使用XML文件中的JavaScript输出所有子子值

时间:2017-12-29 13:38:09

标签: javascript xml

我想通过id搜索书籍并给予 标题作者年份价格值的输出 喜欢

search id = 3

输出:

title: XQuery Kick Start<br>
author: James McGovern<br>
author: Per Bothner<br>
author: Kurt Cagle<br>
author: James Linn<br>
author: Vaidyanathan Nagarajan<br>
year: 2003<br>
price: 49.99<br>

xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
  <book id="1">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book id="2">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book id="3">
    <title lang="un">XQuery Kick Start</title>
    <author>James McGovern</author>
    <author>Per Bothner</author>
    <author>Kurt Cagle</author>
    <author>James Linn</author>
    <author>Vaidyanathan Nagarajan</author>
    <year>2003</year>
    <price>49.99</price>
  </book>
  <book id="4">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>

2 个答案:

答案 0 :(得分:0)

使用DOMParser解析字符串,然后使用getElementByIdquerySelector

var parser = new DOMParser();
var doc = parser.parseFromString( xml, "application/xml" );
var element = doc.getElementById( "1" );
console.log( element.querySelector( "title" ).innerHTML  );

<强>演示

&#13;
&#13;
var xml = `<bookstore>
  <book id="1">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book id="2">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book id="3">
    <title lang="un">XQuery Kick Start</title>
    <author>James McGovern</author>
    <author>Per Bothner</author>
    <author>Kurt Cagle</author>
    <author>James Linn</author>
    <author>Vaidyanathan Nagarajan</author>
    <year>2003</year>
    <price>49.99</price>
  </book>
  <book id="4">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>`;

var parser = new DOMParser();
var doc = parser.parseFromString( xml, "application/xml" );
var element = doc.getElementById( "1" );
console.log( element.querySelector( "title" ).innerHTML  );
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您需要使用DOMParser将字符串转换为XML 之后你在xml中的书search by the id

xmlDoc.getElementById(id)

然后你在书中search by the tags

book.getElementsByTagName(tagName)

所有验证都取决于您。样品:

var text = '<bookstore><book id="1"><title lang="en">Everyday Italian</title><author>Giada De Laurentiis</author><year>2005</year><price>30.00</price></book><book id="2"><title lang="en">Harry Potter</title><author>J K. Rowling</author><year>2005</year><price>29.99</price></book><book id="3"><title lang="un">XQuery Kick Start</title><author>James McGovern</author><author>Per Bothner</author><author>Kurt Cagle</author><author>James Linn</author><author>Vaidyanathan Nagarajan</author><year>2003</year><price>49.99</price></book><book id="4"><title lang="en">Learning XML</title><author>Erik T. Ray</author><year>2003</year><price>39.95</price></book></bookstore>';

parser = new DOMParser();
var xmlDoc = parser.parseFromString(text,"text/xml");

function search(id){
  var book = xmlDoc.getElementById(id);
  
  var title = book.getElementsByTagName("title")[0].innerHTML;
  var year = book.getElementsByTagName("year")[0].innerHTML;
  var price = book.getElementsByTagName("price")[0].innerHTML;
  var authorList = book.getElementsByTagName("author");
  var authors = [];
  Array.prototype.slice.call(authorList).forEach(function(val){
    authors.push(val.innerHTML);
  });
  
  
  var obj = {
    id,
    title,
    year,
    price,
    authors,
  }
  
  console.log(obj);
}

search(3);