XML 文档和有什么区别?XML 字符串? [英] What is the difference between XML document & XML string?

查看:34
本文介绍了XML 文档和有什么区别?XML 字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据之间是否存在内容差异?

Is there any content difference between the data?

也许一个文档可能有额外的元数据,比如作者、最后编辑日期等?

Perhaps a document may have additional metadata like author, last date edited, etc.?

我试图理解为什么 w3 学校的 domparser 示例有两种完全不同的方法来加载 xml 字符串和 xml 文档.

I'm trying to understand why the domparser example at w3 schools has two completely different methods for loading an xml string vs an xml document.

例如,假设两个源都包含 Hello World!.

For example say both sources contained <book><page1>Hello World!</page1></book>.

提供的第一种方法使用 XMLHttpRequest() 和 responseXML 加载 js 对象 xmlDoc,第二种方法使用 DOMParser() 和 parseFromString()

First method offered loads the js object xmlDoc using XMLHttpRequest() and responseXML, the second uses DOMParser() and parseFromString()

每种方法创建的 JS 对象有何不同?

How would the JS objects created by each method differ from one another?

推荐答案

两种方式的结果都是一个代表xml结构的对象.

The result of both ways is an object representing a xml structure.

第一种方法是使用 XMLHttpRequest 从远程位置加载(并解析)一个 xml 文档.

The first way is loading (and parsing) a xml document from a remote location using an XMLHttpRequest.

第二种方法假设您的脚本中已经有 xml 内容(存储在一个变量中,不管它是如何到达那里的).然后 xml 字符串内容将被解析为一个对象,以便您可以轻松地对 xml 数据执行操作(这在字符串上是不可能的)

The second way assumes you already have xml content in your script (stored in a variable, doesn't matter how it got there). The xml string content will then be parsed to an object so you can easily perform operations on the xml data (which isn't possible on the string)

您可以像这样处理生成的 xml 对象:

You can work with the resulting xml object like this:

var p = new DOMParser()
xmldoc = p.parseFromString('<book><page1>Hello World!</page1></book>', 'text/xml')

xmldoc.childNodes.item(0) // the <book> tag
xmldoc.childNodes.item(0).childNodes.item(0) // the <page1> tag
xmldoc.childNodes.item(0).childNodes.item(0).textContent // "Hello World!"

这篇关于XML 文档和有什么区别?XML 字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆