0

我想解析 OpenSearch XML 以获取类似“http://stackoverflow.com/search?q={searchTerms}”的搜索网址

XML是这样的:

<?xml version="1.0" encoding="UTF-8" ?>

<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">
      <ShortName>Stack Overflow</ShortName>
      <Description>Search Stack Overflow: Q&amp;A for professional and enthusiast programmers</Description>
      <InputEncoding>UTF-8</InputEncoding>
      <Image width="16" height="16" type="image/x-icon">http://sstatic.net/stackoverflow/img/favicon.ico</Image>
      <Url type="text/html" method="get" template="http://stackoverflow.com/search?q={searchTerms}"></Url>
</OpenSearchDescription>
4

2 回答 2

0
var domParser = new DOMParser,
    dom = domParser.parseFromString(xml, "application/xml");

console.log( dom.getElementsByTagName("Url")[0].getAttribute("template"));
//"http://stackoverflow.com/search?q={searchTerms}"
于 2012-06-14T08:31:12.950 回答
0

只是让XmlHttpRequest完成工作?我很确定您不会将 XML 作为字符串获取。

var dom = xhr.responseXML;
var url = dom.documentElement.querySelector("Url[template]"); // DOM lvl 3!
if (url)
    return url.getAttribute("template");
于 2012-06-14T13:15:21.083 回答