[英]How to Get Response Data XML From Request API?
I use axios to make it.我使用 axios 来制作它。 How to receive data in XML when I make an request API?
发出请求 API 时如何接收 XML 格式的数据?
Example:例子:
axios.get(/* URL */).then(response => {/* How to get data XML? */}).catch(err => {/* result */});
In your .then
callback, the response
has a property data
it is the xml document as a string.在您的
.then
回调中, response
具有属性data
它是字符串形式的 xml 文档。 Many people would use this string and parse it using a parser that is build into the browser, like it is demonstrated in this very recent question: Reading local text file as XML许多人会使用这个字符串并使用浏览器内置的解析器来解析它,就像最近的这个问题中所演示的那样:将本地文本文件读取为 XML
I however would recomment using a parser library, these provide a much more effective way of working with xml data.但是,我建议使用解析器库,它们提供了一种更有效的处理 xml 数据的方法。 My choice is txml .
我的选择是txml 。 you can get it into your document like this:
您可以像这样将其放入您的文档中:
<script src="https://unpkg.com/txml@4.0.0/tXml.min.js"></script>
Then, when you parse the xml string like this:然后,当您像这样解析 xml 字符串时:
axios.get(/* URL */).then(response => {
const dom = txml.parse(response.data);
}).catch(err => {/* result */});
The constant dom, is an array, of xml nodes.常量 dom 是一个由 xml 节点组成的数组。 each node has the properties
tagName
, attributes
and children
.每个节点都有属性
tagName
、 attributes
和children
。 children
is again an array of nodes. children
又是一个节点数组。
the library has very handy helper functions like 'txml.simplify(dom)' will give you an object, that looks more like a data object.该库具有非常方便的辅助函数,例如 'txml.simplify(dom)' 会给你一个对象,它看起来更像一个数据对象。 you can check the API documentation on npm .
您可以查看npm上的 API 文档。
DISCLAIMER: I am the author of txml.免责声明:我是 txml 的作者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.