Office.CustomXmlNode interface

表示文档中的树中的 XML 节点。

注解

应用程序:Word

属性

baseName

获取不带命名空间前缀的节点的基名称(如有)。

namespaceUri

检索 CustomXMLPart 的字符串 GUID。

nodeType

获取 CustomXMLNode 的类型。

方法

getNodesAsync(xPath, options, callback)

获取与 XPath 表达式关联的节点。

getNodesAsync(xPath, callback)

获取与 XPath 表达式关联的节点。

getNodeValueAsync(options, callback)

获取节点值。

getNodeValueAsync(callback)

获取节点值。

getTextAsync(options, callback)

获取自定义 XML 部件中 XML 节点的文本。

getTextAsync(callback)

获取自定义 XML 部件中 XML 节点的文本。

getXmlAsync(options, callback)

获取节点的 XML。

getXmlAsync(callback)

获取节点的 XML。

setNodeValueAsync(value, options, callback)

设置节点值。

setNodeValueAsync(value, callback)

设置节点值。

setTextAsync(text, options, callback)

异步设置自定义 XML 部件中 XML 节点的文本。

setTextAsync(text, callback)

异步设置自定义 XML 部件中 XML 节点的文本。

setXmlAsync(xml, options, callback)

设置节点 XML。

setXmlAsync(xml, callback)

设置节点 XML。

属性详细信息

baseName

获取不带命名空间前缀的节点的基名称(如有)。

baseName: string;

属性值

string

示例

function showXmlNodeBaseNames() {
    Office.context.document.customXmlParts.getByIdAsync(
        "{3BC85265-09D6-4205-B665-8EB239A8B9A1}", function (result) {
        const xmlPart = result.value;
        xmlPart.getNodesAsync('*/*', function (nodeResults) {
            for (let i = 0; i < nodeResults.value.length; i++) {
                const node = nodeResults.value[i];
                write(node.baseName);
            }
        });
    });
}
// Function that writes to a div with id='message' on the page.
function write(message){
    document.getElementById('message').innerText += message; 
}

namespaceUri

检索 CustomXMLPart 的字符串 GUID。

namespaceUri: string;

属性值

string

示例

function showXmlNamespaceUri() {
    Office.context.document.customXmlParts.getByIdAsync(
        "{3BC85265-09D6-4205-B665-8EB239A8B9A1}", function (result) {
        const xmlPart = result.value;
        xmlPart.getNodesAsync('*/*', function (nodeResults) {
            for (let i = 0; i < nodeResults.value.length; i++) {
                const node = nodeResults.value[i];
                write(node.namespaceUri);
            }
        });
    });
}
// Function that writes to a div with id='message' on the page.
function write(message){
    document.getElementById('message').innerText += message; 
}

nodeType

获取 CustomXMLNode 的类型。

nodeType: string;

属性值

string

示例

function showXmlNodeType() {
    Office.context.document.customXmlParts.getByIdAsync(
        "{3BC85265-09D6-4205-B665-8EB239A8B9A1}", function (result) {
        const xmlPart = result.value;
        xmlPart.getNodesAsync('*/*', function (nodeResults) {
            for (let i = 0; i < nodeResults.value.length; i++) {
                const node = nodeResults.value[i];
                write(node.nodeType);
            }
        });
    });
}
// Function that writes to a div with id='message' on the page.
function write(message){
    document.getElementById('message').innerText += message; 
}

方法详细信息

getNodesAsync(xPath, options, callback)

获取与 XPath 表达式关联的节点。

getNodesAsync(xPath: string, options?: Office.AsyncContextOptions, callback?: (result: AsyncResult<CustomXmlNode[]>) => void): void;

参数

xPath

string

指定要获取的节点的 XPath 表达式。 必填。

options
Office.AsyncContextOptions

提供一个选项,用于保留任何类型的上下文数据(保持不变),以便在回调中使用。

callback

(result: Office.AsyncResult<Office.CustomXmlNode[]>) => void

可选。 回调返回时调用的函数,其唯一参数的类型为 Office.AsyncResultvalue结果的 属性是 CustomXmlNode 对象的数组,这些对象表示传递给 xPath 参数的 XPath 表达式指定的节点。

返回

void

注解

要求集CustomXmlParts

getNodesAsync(xPath, callback)

获取与 XPath 表达式关联的节点。

getNodesAsync(xPath: string, callback?: (result: AsyncResult<CustomXmlNode[]>) => void): void;

参数

xPath

string

指定要获取的节点的 XPath 表达式。 必填。

callback

(result: Office.AsyncResult<Office.CustomXmlNode[]>) => void

可选。 回调返回时调用的函数,其唯一参数的类型为 Office.AsyncResultvalue结果的 属性是 CustomXmlNode 对象的数组,这些对象表示传递给 xPath 参数的 XPath 表达式指定的节点。

返回

void

注解

要求集CustomXmlParts

示例

function showXmlChildNodes() {
    Office.context.document.customXmlParts.getByIdAsync(
        "{3BC85265-09D6-4205-B665-8EB239A8B9A1}", function (result) {
        const xmlPart = result.value;
        xmlPart.getNodesAsync('*', function (nodeResults) {
            for (let i = 0; i < nodeResults.value.length; i++) {
                const node = nodeResults.value[i];
                node.getNodesAsync('*', function (nodeResults) {
                    write(nodeResults.value.length + " childNodes");
                });
            }
        });
    });
}
// Function that writes to a div with id='message' on the page.
function write(message){
    document.getElementById('message').innerText += message; 
}

getNodeValueAsync(options, callback)

获取节点值。

getNodeValueAsync(options?: Office.AsyncContextOptions, callback?: (result: AsyncResult<string>) => void): void;

参数

options
Office.AsyncContextOptions

提供一个选项,用于保留任何类型的上下文数据(保持不变),以便在回调中使用。

callback

(result: Office.AsyncResult<string>) => void

可选。 回调返回时调用的函数,其唯一参数的类型为 Office.AsyncResultvalue结果的 属性是一个字符串,其中包含所引用节点的值。

返回

void

注解

要求集CustomXmlParts

getNodeValueAsync(callback)

获取节点值。

getNodeValueAsync(callback?: (result: AsyncResult<string>) => void): void;

参数

callback

(result: Office.AsyncResult<string>) => void

可选。 回调返回时调用的函数,其唯一参数的类型为 Office.AsyncResultvalue结果的 属性是一个字符串,其中包含所引用节点的值。

返回

void

注解

要求集CustomXmlParts

示例

function showXmlNodeValues() {
    Office.context.document.customXmlParts.getByIdAsync(
        "{3BC85265-09D6-4205-B665-8EB239A8B9A1}", function (result) {
        const xmlPart = result.value;
        xmlPart.getNodesAsync('*/*', function (nodeResults) {
            for (let i = 0; i < nodeResults.value.length; i++) {
                const node = nodeResults.value[i];
                node.getNodeValueAsync(function (asyncResult) {
                    write(asyncResult.value);
                });
            }
        });
    });
}
// Function that writes to a div with id='message' on the page.
function write(message){
    document.getElementById('message').innerText += message; 
}

getTextAsync(options, callback)

获取自定义 XML 部件中 XML 节点的文本。

getTextAsync(options?: Office.AsyncContextOptions, callback?: (result: AsyncResult<string>) => void): void;

参数

options
Office.AsyncContextOptions

提供一个选项,用于保留任何类型的上下文数据(保持不变),以便在回调中使用。

callback

(result: Office.AsyncResult<string>) => void

可选。 回调返回时调用的函数,其唯一参数的类型为 Office.AsyncResultvalue结果的 属性是包含所引用节点的内部文本的字符串。

返回

void

注解

要求集CustomXmlParts

示例

// Get the built-in core properties XML part by using its ID. This results in a call to Word.
Office.context.document.customXmlParts.getByIdAsync(
    "{6C3C8BC8-F283-45AE-878A-BAB7291924A1}", function (getByIdAsyncResult) {
    
    // Access the XML part.
    const xmlPart = getByIdAsyncResult.value;
    
    // Add namespaces to the namespace manager. These two calls result in two calls to Word.
    xmlPart.namespaceManager.addNamespaceAsync(
        'cp',
        'http://schemas.openxmlformats.org/package/2006/metadata/core-properties',
        function () {
        xmlPart.namespaceManager.addNamespaceAsync(
            'dc', 
            'http://purl.org/dc/elements/1.1/', 
            function () {

            // Get XML nodes by using an Xpath expression. This results in a call to Word.
            xmlPart.getNodesAsync("/cp:coreProperties/dc:title", function (getNodesAsyncResult) {
                
                // Get the first node returned by using the Xpath expression. 
                const node = getNodesAsyncResult.value[0];
                
                // Get the text value of the node and use the asyncContext. This results in a call to Word. 
                // The results are logged to the browser console.
                node.getTextAsync({asyncContext: "StateNormal"}, function (getTextAsyncResult) {
                  console.log("Text of the title element = " + getTextAsyncResult.value;
                  console.log("The asyncContext value = " + getTextAsyncResult.asyncContext;
                });
            });
        });
    });
});

getTextAsync(callback)

获取自定义 XML 部件中 XML 节点的文本。

getTextAsync(callback?: (result: AsyncResult<string>) => void): void;

参数

callback

(result: Office.AsyncResult<string>) => void

可选。 回调返回时调用的函数,其唯一参数的类型为 Office.AsyncResultvalue结果的 属性是包含所引用节点的内部文本的字符串。

返回

void

注解

要求集CustomXmlParts

getXmlAsync(options, callback)

获取节点的 XML。

getXmlAsync(options?: Office.AsyncContextOptions, callback?: (result: AsyncResult<string>) => void): void;

参数

options
Office.AsyncContextOptions

提供一个选项,用于保留任何类型的上下文数据(保持不变),以便在回调中使用。

callback

(result: Office.AsyncResult<string>) => void

可选。 回调返回时调用的函数,其唯一参数的类型为 Office.AsyncResultvalue结果的 属性是包含所引用节点的 XML 的字符串。

返回

void

注解

要求集CustomXmlParts

getXmlAsync(callback)

获取节点的 XML。

getXmlAsync(callback?: (result: AsyncResult<string>) => void): void;

参数

callback

(result: Office.AsyncResult<string>) => void

可选。 回调返回时调用的函数,其唯一参数的类型为 Office.AsyncResultvalue结果的 属性是包含所引用节点的 XML 的字符串。

返回

void

注解

要求集CustomXmlParts

示例

function showXmlNodeInnerXml() {
    Office.context.document.customXmlParts.getByIdAsync(
        "{3BC85265-09D6-4205-B665-8EB239A8B9A1}", function (result) {
        const xmlPart = result.value;
        xmlPart.getNodesAsync('*', function (nodeResults) {
            for (let i = 0; i < nodeResults.value.length; i++) {
                const node = nodeResults.value[i];
                node.getXmlAsync(function (asyncResult) {
                    write(asyncResult.value);
                });
            }
        });
    });
}
// Function that writes to a div with id='message' on the page.
function write(message){
    document.getElementById('message').innerText += message; 
}

setNodeValueAsync(value, options, callback)

设置节点值。

setNodeValueAsync(value: string, options?: Office.AsyncContextOptions, callback?: (result: AsyncResult<void>) => void): void;

参数

value

string

要在节点上设置的值

options
Office.AsyncContextOptions

提供一个选项,用于保留任何类型的上下文数据(保持不变),以便在回调中使用。

callback

(result: Office.AsyncResult<void>) => void

可选。 回调返回时调用的函数,其唯一参数的类型为 Office.AsyncResult

返回

void

注解

要求集CustomXmlParts

setNodeValueAsync(value, callback)

设置节点值。

setNodeValueAsync(value: string, callback?: (result: AsyncResult<void>) => void): void;

参数

value

string

要在节点上设置的值

callback

(result: Office.AsyncResult<void>) => void

可选。 回调返回时调用的函数,其唯一参数的类型为 Office.AsyncResult

返回

void

注解

要求集CustomXmlParts

示例

function setXmlNodeValue() {
    Office.context.document.customXmlParts.getByIdAsync(
        "{3BC85265-09D6-4205-B665-8EB239A8B9A1}", function (result) {
        const xmlPart = result.value;
        xmlPart.getNodesAsync('*/*', function (nodeResults) {
            for (let i = 0; i < nodeResults.value.length; i++) {
                const node = nodeResults.value[i];
                write(node);
                node.setNodeValueAsync("item number" + i, function (result) { });
            }
        });
    });
}
// Function that writes to a div with id='message' on the page.
function write(message){
    document.getElementById('message').innerText += message; 
}

setTextAsync(text, options, callback)

异步设置自定义 XML 部件中 XML 节点的文本。

setTextAsync(text: string, options?: Office.AsyncContextOptions, callback?: (result: AsyncResult<void>) => void): void;

参数

text

string

必填。 XML 节点的文本值。

options
Office.AsyncContextOptions

提供一个选项,用于保留任何类型的上下文数据(保持不变),以便在回调中使用。

callback

(result: Office.AsyncResult<void>) => void

可选。 回调返回时调用的函数,其唯一参数的类型为 Office.AsyncResult

返回

void

注解

要求集CustomXmlParts

示例

// Learn how to set the text value of a node in a custom XML part from the following example.

// Get the built-in core properties XML part by using its ID. This results in a call to Word.
Office.context.document.customXmlParts.getByIdAsync(
    "{6C3C8BC8-F283-45AE-878A-BAB7291924A1}",
    function (getByIdAsyncResult) {
    
    // Access the XML part.
    const xmlPart = getByIdAsyncResult.value;
    
    // Add namespaces to the namespace manager. These two calls result in two calls to Word.
    xmlPart.namespaceManager.addNamespaceAsync(
        'cp', 
        'http://schemas.openxmlformats.org/package/2006/metadata/core-properties', 
        function () {
        xmlPart.namespaceManager.addNamespaceAsync(
            'dc', 
            'http://purl.org/dc/elements/1.1/', 
            function () {

            // Get XML nodes by using an Xpath expression. This results in a call to the host.
            xmlPart.getNodesAsync("/cp:coreProperties/dc:subject", function (getNodesAsyncResult) {
                
                // Get the first node returned by using the Xpath expression.
                // This will be the subject element in this example.
                const subjectNode = getNodesAsyncResult.value[0];
                
                // Set the text value of the subject node and use the asyncContext. 
                // This results in a call to the host.  The results are logged to the browser console.
                subjectNode.setTextAsync(
                    "newSubject", 
                    {asyncContext: "StateNormal"}, 
                    function (setTextAsyncResult) {
                        console.log("The status of the call: " + setTextAsyncResult.status);
                        console.log("The asyncContext value = " + setTextAsyncResult.asyncContext);
                });
            });
        });
    });
});

setTextAsync(text, callback)

异步设置自定义 XML 部件中 XML 节点的文本。

setTextAsync(text: string, callback?: (result: AsyncResult<void>) => void): void;

参数

text

string

必填。 XML 节点的文本值。

callback

(result: Office.AsyncResult<void>) => void

可选。 回调返回时调用的函数,其唯一参数的类型为 Office.AsyncResult

返回

void

注解

要求集CustomXmlParts

setXmlAsync(xml, options, callback)

设置节点 XML。

setXmlAsync(xml: string, options?: Office.AsyncContextOptions, callback?: (result: AsyncResult<void>) => void): void;

参数

xml

string

在节点上设置的 XML

options
Office.AsyncContextOptions

提供一个选项,用于保留任何类型的上下文数据(保持不变),以便在回调中使用。

callback

(result: Office.AsyncResult<void>) => void

可选。 回调返回时调用的函数,其唯一参数的类型为 Office.AsyncResult

返回

void

注解

要求集CustomXmlParts

示例

function setXmlNodeInnerXml() {
    Office.context.document.customXmlParts.getByIdAsync(
        "{3BC85265-09D6-4205-B665-8EB239A8B9A1}", function (result) {
        const xmlPart = result.value;
        xmlPart.getNodesAsync('*', function (nodeResults) {
            for (let i = 0; i < nodeResults.value.length; i++) {
                const node = nodeResults.value[i];
                node.setXmlAsync("<childNode>" + i + "</childNode>");
            }
        });
    });
}

setXmlAsync(xml, callback)

设置节点 XML。

setXmlAsync(xml: string, callback?: (result: AsyncResult<void>) => void): void;

参数

xml

string

在节点上设置的 XML

callback

(result: Office.AsyncResult<void>) => void

可选。 回调返回时调用的函数,其唯一参数的类型为 Office.AsyncResult

返回

void

注解

要求集CustomXmlParts