简体   繁体   English

利用内置的XPath引擎查询javaScript对象

[英]Exploit the built in XPath engine to query javaScript objects

NOTE: I am not looking for a way to query the HTML document itself. 注意:我不是在寻找一种查询HTML文档本身的方法。 I want to create my own document from my javaScript object and pass it as root argument to the evaluate function. 我想从我的javaScript对象创建我自己的文档,并将其作为根参数传递给evaluate函数。

Say I have the following script: 说我有以下脚本:

function Attribute(name, value) {
    this.name;
    this.value;
}

function Node(nodeName) {
    this.nodeName = nodeName;
    this.textContent = "";
    this.childrenNodes = [];
    this.attributes = [];
}

var root = new Node("root");
root.attributes.push(new Attribute("name", "treeRoot"));

var c1 = new Node("child");
c1.attributes.push(new Attribute("name", "child1"));
c1.textContent = "I'm the first child!";

var c2 = new Node("child");
c2.attributes.push(new Attribute("name", "child2"));

root.childrenNodes.push(c1);
root.childrenNodes.push(c2);

That represents the following simple XML: 这表示以下简单的XML:

<root name="treeRoot">
    <child name="child1">
        I'm the first child!
    </child>
    <child name="child2"/>
</root>

I would like to use the build in XPath engine to query this XML-like structure. 我想使用XPath引擎中的构建来查询这种类似XML的结构。 Somthing like: 有点像:

myDocument = createDocument(root);
myDocument.evaluate("/root/child[@name='child2']", myDocument, null, XPathResult.ANY_TYPE, null);

That would return an XPathResult of Node collection containing c1 Node . 这将返回包含c1 NodeNode集合的XPathResult

How do I implement the createDocument function? 如何实现createDocument函数?

EDIT: 编辑:

My goal is to be able to query javaScript objects. 我的目标是能够查询javaScript对象。 In Java I can create a Document object and use XPath to query it. 在Java中,我可以创建一个Document对象并使用XPath对其进行查询。 I'm looking for something similar in javaScript. 我正在寻找类似javaScript的东西。

You need a couple of functions here to convert your "DOM" implementation to a standard XML DOM - one to create the document and another to recursively create elements: 您在这里需要几个函数将“ DOM”实现转换为标准XML DOM-一个用于创建文档,另一个用于递归创建元素:

// create a document based on a Node instance
function toXmlDom(node) {
    // create a document
    var doc = document.implementation.createDocument('', '');

    // convert the root node
    var e = toXmlElement(doc, node);

    // add root to document
    doc.appendChild(e);
    return doc;
}

// convert a Node and its children to an XML element
function toXmlElement(doc, node) {
    // create an element
    var e = doc.createElement(node.nodeName);

    // set its attributes
    for(var i = 0; i < node.attributes.length; i++) {
        var attr = node.attributes[i];
        e.setAttribute(attr.name, attr.value);
    }

    // set its text content
    e.textContent = node.textContent;

    // convert and add its child nodes
    for(var i = 0; i < node.childrenNodes.length; i++) {
        var childrenNode = node.childrenNodes[i];
        var childNode = toXmlElement(doc, childrenNode);
        e.appendChild(childNode);
    }

    return e;
}

// do the conversion
var myDocument = toXmlDom(root);

Working Example 工作实例

 console.clear(); function Attribute(name, value) { this.name = name; this.value = value; } function Node(nodeName) { this.nodeName = nodeName; this.textContent = ""; this.childrenNodes = []; this.attributes = []; } function toXmlDom(node) { // create a document var doc = document.implementation.createDocument('', ''); // convert the root node var e = toXmlElement(doc, node); // add root to document doc.appendChild(e); return doc; } function toXmlElement(doc, node) { // create an element var e = doc.createElement(node.nodeName); // set its attributes for(var i = 0; i < node.attributes.length; i++) { var attr = node.attributes[i]; e.setAttribute(attr.name, attr.value); } // set its text content e.textContent = node.textContent; // convert and add its child nodes for(var i = 0; i < node.childrenNodes.length; i++) { var childrenNode = node.childrenNodes[i]; var childNode = toXmlElement(doc, childrenNode); e.appendChild(childNode); } return e; } var root = new Node("root"); root.attributes.push(new Attribute("name", "treeRoot")); var c1 = new Node("child"); c1.attributes.push(new Attribute("name", "child1")); c1.textContent = "I'm the first child!"; var c2 = new Node("child"); c2.attributes.push(new Attribute("name", "child2")); root.childrenNodes.push(c1); root.childrenNodes.push(c2); var myDocument = toXmlDom(root); // get the text of the first child - "I'm the first child!" var result = myDocument.evaluate("/root/child[@name='child1']", myDocument, null, XPathResult.ANY_TYPE, null); var thisNode = result.iterateNext(); while (thisNode) { document.getElementById('result').innerHTML += thisNode.textContent + "<br/>"; thisNode = result.iterateNext(); } document.getElementById('doctext').value = myDocument.documentElement.outerHTML; 
 <p><b>/root/child[@name='child1'].textContent:</b> <span id="result"></span></p> <b>Document XML</b><br/> <textarea id="doctext" cols="50" rows="10"></textarea> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM