簡體   English   中英

利用內置的XPath引擎查詢javaScript對象

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

注意:我不是在尋找一種查詢HTML文檔本身的方法。 我想從我的javaScript對象創建我自己的文檔,並將其作為根參數傳遞給evaluate函數。

說我有以下腳本:

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);

這表示以下簡單的XML:

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

我想使用XPath引擎中的構建來查詢這種類似XML的結構。 有點像:

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

這將返回包含c1 NodeNode集合的XPathResult

如何實現createDocument函數?

編輯:

我的目標是能夠查詢javaScript對象。 在Java中,我可以創建一個Document對象並使用XPath對其進行查詢。 我正在尋找類似javaScript的東西。

您在這里需要幾個函數將“ 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);

工作實例

 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