如何使用 JavaScript 从 XML 调用特定条目



所以我有这个XML代码

<book>
<bookname>book1</bookname>
<author>authorman</author>
</book>
<book>
<bookname>book2</bookname>
<author>authorperson</author>
</book>

我希望能够根据作者的名字使用作者获取书名,反之亦然,并使用 JavaScript 将其分配给变量。 我试图避免使用服务器端编程,只想使用 JavaScript。 这是我第一次在这里发帖,因为我是一名最近学习编程的高中生。

您可以将XMLHttpRequest()responseType属性设置为"document"overrideMimeType()设置为"text/html"以获取 HTML#documentload处理程序中的response

创建一个函数,该函数期望将纯对象作为参数传递,具有"author""bookname"属性,其值设置为XML<book>元素textContent<bookname><author>匹配。迭代<book>元素和childrenHTMLCollectionNodeList,检查localName是否等于"bookname""author",并且textContent匹配传递给函数的参数,如果为 true,则获取parentElement引用,如果"author"是对象参数的属性,则使用.querySelector()传递"bookname", 或"author"如果"bookname"作为参数传递,则返回匹配元素的textContent,或'"author of <bookname> OR book by <author>" not found.'

const request = new XMLHttpRequest();
request.responseType = "document";
request.overrideMimeType("text/html");
const xml = `<?xml version="1.0" encoding="UTF-8"?><book>
<bookname>book1</bookname>
<author>authorman</author>
</book>
<book>
<bookname>book2</bookname>
<author>authorperson</author>
</book>`;
const getBookData = (books, {bookname, author} = {}) => {
for (const {children} of books) { 
for (const book of children) {
const {localName, textContent} = book;       
if (localName === "bookname" && bookname === textContent 
|| localName === "author" && author === textContent) {
return book.parentElement
.querySelector(author ? "bookname" : "author")
.textContent
}
}
}
return `${author ? "book by" : "author of"} "${bookname || author}" not found in library.`;
}
request.addEventListener("load", e => {
const html = request.response;
const books = html.querySelectorAll("book");
console.log(getBookData(books, {author:"authorman"}));
console.log(getBookData(books, {bookname:"book2"}));
console.log(getBookData(books, {author:"authorx"}));
console.log(getBookData(books, {bookname:"book3"}));
})
request.open("GET", `data:application/xml,${encodeURIComponent(xml)}`);
request.send();

试试这个

var text, parser, xmlDoc;  
text="<book>
<bookname>book1</bookname>
<author>authorman</author>
</book>
<book>
<bookname>book2</bookname>
<author>authorperson</author>
</book>";  
parser = new DOMParser();
xmlDoc = parser.parseFromString(text,"text/xml");
console.log(xmlDoc.getElementsByTagName("book")[0].childNodes[0].nodeValue); //book1
console.log(xmlDoc.getElementsByTagName("book")[1].childNodes[0].nodeValue); //book2

希望这对您有所帮助:-

<script type="text/javascript">
var xml = "<xml>"+
"<book>"+
"<bookname>book1</bookname>"+
"<author>authorman</author>"+
"</book>"+
"<book>"+
"<bookname>book2</bookname>"+
"<author>author2</author>"+
"</book>"+
"</xml>";
parser = new DOMParser();
xmlDoc = parser.parseFromString(xml,"text/xml");
authors   = xmlDoc.getElementsByTagName("author");
booknames = xmlDoc.getElementsByTagName("bookname");
var bookname = searchByAuthor('authorman', authors, booknames);
alert(bookname);
var authorname = searchByBookName('book2', authors, booknames);
alert(authorname);

function searchByAuthor(author_name, x, y){

for (i = 0; i < x.length; i++) {
if(x[i].childNodes[0].nodeValue == author_name){
return y[i].childNodes[0].nodeValue;
}  
}
}
function searchByBookName(book_name, x, y){
for (i = 0; i < y.length; i++) {
if(y[i].childNodes[0].nodeValue == book_name){
return x[i].childNodes[0].nodeValue;
}  
}
}
</script>

最新更新