dom parser
来源:互联网 发布:js如何把div隐藏 编辑:程序博客网 时间:2024/06/05 00:52
try{ var strXml="<si><indication href=\"http://www.baidu.com\" si-id=\"1\" action=\"signal-low\">Medium</indication><indication href=\"http://www.taobao.com\" action=\"delete\"></indication></si>"; //在gecko中使用 To create a DOMParser when the constructor is not available (e.g., from a JS XPCOM component, a JS module, or an xpcshell test), use: //var domParser = Cc["@mozilla.org/xmlextras/domparser;1"] //.createInstance(Ci.nsIDOMParser); var domParser1 = new DOMParser(); var aDom1 = domParser1.parseFromString(strXml, "application/xml"); // get a list of Nodes whos' tag is 'indication'; var feedList1 = aDom1.getElementsByTagName("indication"); alert(feedList1.length); if(feedList1.length>0){ var value1=feedList1[0].attributes["action"].value; alert(value1); } //get all nodes whos tag is indication , attribute action=== delete var indicationNodes=aDom1.querySelectorAll('indication[action="delete"]'); for (var i =0; i < indicationNodes.length; i++){ var indicationNode=indicationNodes[i]; var href=indicationNode.getAttribute('href'); alert(href); } //get the first node whos tag is indication. var indicationNode=aDom1.querySelector('indication'); alert(indicationNode.getAttribute('href'));}catch (error){ alert(error);}
ns
I D O M Parser IN THIS ARTICLE
- Creating a DOMParser
- Principals, document and base URI
- Parsing a string
- Examples
Creating a DOMParser
To create a
DOMParser
object from a web page or a chrome script running in a window, simply usenew DOMParser()
. When you create aDOMParser
from a privileged script, you can pass parameters to the constructor, more on that below.To create a
DOMParser
when the constructor is not available (e.g., from a JS XPCOM component, a JS module, or an xpcshell test), use:var parser = Components.classes["@mozilla.org/xmlextras/domparser;1"] .createInstance(Components.interfaces.nsIDOMParser);// optionally, call parser.init(principal, documentURI, baseURI);
Principals, document and base URI
(This section is only relevant to Firefox extensions--not to Web content.)
To create a document, the parser needs to specify a principal (see Security check basics), a base URI (see document.baseURIObject), and a documentURI.
These values are automatically determined as defined below, but if you work with
DOMParser
from privileged code, you can override the defaults by providing arguments to the DOMParser constructor or callingparser.init()
. Usually you don't need to do that. If you come across a situation when these matter, feel free to ask questions in mozilla.dev.tech.dom and update this documentation to mention these cases.
- When a
DOMParser
is instantiated by callingnew DOMParser()
, it inherits the calling code's principal (except that for chrome callers the principal is set to the null principal) and thedocumentURI
andbaseURI
of the window the constructor came from.- If the caller has UniversalXPConnect privileges, it can pass parameters to
new DOMParser()
. If fewer than three parameters are passed, the remaining parameters will default tonull
.
- The first parameter is the principal to use; this overrides the default principal normally inherited.
- The second parameter is the
documentURI
to use.- The third parameter is the
baseURI
to use.- If you instantiate a
DOMParser
by callingcreateInstance()
, and you don't call theDOMParser
'sinit()
method, attempting to initiate a parsing operation will automatically callinit()
on theDOMParser
with a null principal andnull
pointers fordocumentURI
andbaseURI
.Cases where these values matter:
- If you don't specify the document URI by calling init() after creating the parser via
createInstance()
the created documents will use a moz-nullprincipal:{<guid>} URI, which will show in the Error Console in parsing errors, in particular.- Supposedly, if you want to create objects that some particular set of unprivileged code will be able to access (see discussion in bug 565480).
Parsing a string
Once you've created a
DOMParser
object, you can use itsparseFromString
method to parse XML or HTML as described in the Web platform documentation.Examples
Within the context of a window:
var parser = new DOMParser();var doc = parser.parseFromString(aStr, "application/xml");
Outside of a window (e.g., a JS XPCOM component, a JS module, or an xpcshell test):
var parser = Components.classes["@mozilla.org/xmlextras/domparser;1"] .createInstance(Components.interfaces.nsIDOMParser);var doc = parser.parseFromString(aStr, "application/xml");
Using
Components.Constructor()
:const DOMParser = new Components.Constructor("@mozilla.org/xmlextras/domparser;1", "nsIDOMParser");var parser = new DOMParser();parser.init(principal, documentURI, baseURI);var doc = parser.parseFromString(aStr, "application/xml");
- dom parser
- read XML-DOM parser
- write XML-DOM Parser
- XML DOM Parser
- RTF Dom Parser
- Simple Html Dom Parser的使用
- Easynet WinINET Imp. & HTML DOM Parser
- Java Reading XML using DOM parser
- UI 13 数据解析 Parser DOM JSON
- 【PHP HTML DOM解析器】PHP Simple HTML DOM Parser
- PHP Simple HTML DOM Parser 強力解析html工具
- PHP Simple HTML DOM Parser的编码问题
- php的html解析工具 PHP Simple HTML DOM Parser
- 使用php simple html dom parser解析html标签
- How To Read XML File In Java – (DOM Parser)
- parsing XML using DOM, SAX and StAX Parser in Java
- How To Create XML File In Java – (DOM Parser)
- 【Java编程】DOM XML Parser解析、遍历、创建XML
- sql查找表中某个字段值相同,其他字段取最大值的记录
- 算法导论 练习题 17.3-6
- opencv3.2安装opencv_contrib
- 线程池控制输出CountDownLatch同步计数器
- phabricator实战使用
- dom parser
- "Your device isn't compatible with this version" 部分设备无法从google play下载
- 去除js数组中的重复元素
- 正则表达式 匹配字母和数字
- Linux下PCI调试指令的lspci
- html中rem的基本代码
- MUI对话框
- phabricator
- 【初学】VS2013连接SQL Server 2008 R2测试