createProcessingInstruction Method

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - DOM Reference

createProcessingInstruction Method

Creates a processing instruction node that contains the supplied target and data.

[Script]

Script Syntax

var objXMLDOMProcessingInstruction = 
oXMLDOMDocument.createProcessingInstruction(target, data);

Parameters

target
A string specifying the target part of the processing instruction. This supplies the nodeName property of the new object.
data
A string specifying the rest of the processing instruction preceding the closing ?> characters. This supplies the nodeValue property for the new object.

Return Value

An object. Returns the new IXMLDOMProcessingInstruction object.

Example

The following script example specifies the target string "xml" and the data string "version=\"1.0\"" to generate the processing instruction <?XML version="1.0"?>.

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.5.0");
var pi;
xmlDoc.async = false;
xmlDoc.load("books.xml");
if (xmlDoc.parseError.errorCode <> 0) {
   var myErr = xmlDoc.parseError;
   alert("You have error " + myErr.reason);
} else {
   pi = xmlDoc.createProcessingInstruction("xml", "version=\"1.0\"");
   xmlDoc.insertBefore(pi, xmlDoc.childNodes.item(0));
   alert(xmlDoc.xml);
}
[Visual Basic]

Visual Basic Syntax

Set objXMLDOMProcessingInstruction = 
oXMLDOMDocument.createProcessingInstruction(target, data)

Parameters

target
A string specifying the target part of the processing instruction. This supplies the nodeName property of the new object.
data
A string specifying the rest of the processing instruction preceding the closing ?> characters. This supplies the nodeValue property for the new object.

Return Value

An object. Returns the new IXMLDOMProcessingInstruction object.

Example

The following Microsoft® Visual Basic® example specifies the target string "xml" and data string "version = \"1.0\"" to generate the processing instruction <?XML version="1.0"?>.

Dim xmlDoc As New Msxml2.DOMDocument50
Dim pi As IXMLDOMProcessingInstruction
xmlDoc.async = False
xmlDoc.Load ("books.xml")
If (xmlDoc.parseError.errorCode <> 0) Then
   Dim myErr
   Set myErr = xmlDoc.parseError
   MsgBox("You have error " & myErr.reason)
Else
   Set pi = xmlDoc.createProcessingInstruction("xml", "version=\"1.0\"")
   xmlDoc.insertBefore pi, xmlDoc.childNodes.Item(0)
   MsgBox (xmlDoc.xml)
End If
[C/C++]

C/C++ Syntax

HRESULT createProcessingInstruction(
    BSTR target,
    BSTR data,
    IXMLDOMProcessingInstruction **pi);

Parameters

target [in]
The target part of the processing instruction. It supplies the nodeName property of the new object.
data [in]
The remainder of the processing instruction preceding the closing ?> characters. It supplies the nodeValue property for the new object.
pi [out, retval]
The address of the new IXMLDOMProcessingInstruction object.

C/C++ Return Values

S_OK
The value returned if successful.
E_INVALIDARG
The value returned if the pi parameter is Null.
E_FAIL
The value returned if an error occurs.

Remarks

Creating a processing instruction node with this method is the same as using createNode where the Type parameter value is NODE_PROCESSING_INSTRUCTION and no namespace is specified. You cannot specify a namespace with the createProcessingInstruction method.

Although this method creates the new object in the context of this document, it does not automatically add the new object to the document tree. In other words, although the ownerDocument property of the new node points to this document object, the parentNode property is set to Null. To add the new object, you must explicitly call one of the node insert methods, insertBefore method, replaceChild method, or appendChild method.

The new object's nodeType property has the value NODE_PROCESSING_INSTRUCTION.

To view reference information for Visual Basic, C/C++, or Script only, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

nodeName Property | nodeType Property | nodeValue Property | IXMLDOMProcessingInstruction | createNode Method | ownerDocument Property | insertBefore Method | replaceChild Method | appendChild Method

Applies to: DOMDocument