共用方式為


IXMLDOMNode

 

Extends the core node with support for data types, namespaces, document type definitions (DTDs), and schemas.

Example

The following script example uses books.xml to create an IXMLDOMNode, currNode, and display its XML.

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
var currNode;
xmlDoc.async = false;
xmlDoc.load("books.xml");
if (xmlDoc.parseError.errorCode != 0) {
   var myErr = xmlDoc.parseError;
   WScript.Echo("You have error " + myErr.reason);
} else {
   currNode = xmlDoc.documentElement.childNodes.item(1);
   WScript.Echo (currNode.xml);
}

Output

<book id="bk102">

<author>Ralls, Kim</author>

<title>Midnight Rain</title>

<genre>Fantasy</genre>

<price>5.95</price>

<publish_date>2000-12-16</publish_date>

<description>A former architect battles corporate zombies,

an evil sorceress, and her own childhood to become queen

of the world.</description>

</book>

Example

The following Microsoft® Visual Basic® example uses books.xml to create an IXMLDOMNode, currNode, and display its XML.

Dim xmlDoc As New Msxml2.DOMDocument30
Dim currNode As IXMLDOMNode
xmlDoc.async = False
xmlDoc.Load App.Path & "\books.xml"
If (xmlDoc.parseError.errorCode <> 0) Then
   Dim myErr
   Set myErr = xmlDoc.parseError
   MsgBox("You have error " & myErr.reason)
Else
   Set currNode = xmlDoc.documentElement.childNodes.Item(1)
   MsgBox currNode.xml
End If

Output

<book id="bk102">

<author>Ralls, Kim</author>

<title>Midnight Rain</title>

<genre>Fantasy</genre>

<price>5.95</price>

<publish_date>2000-12-16</publish_date>

<description>A former architect battles corporate zombies,

an evil sorceress, and her own childhood to become queen

of the world.</description>

</book>

Example

The following C/C++ example creates and appends a new node to the document root.

#include <tchar.h>
#import "msxml6.dll"
using namespace MSXML2;

inline void TESTHR( HRESULT _hr ) 
   { if FAILED(_hr) throw(_hr); }

void XMLDOMNodeSample()
{
    try {
        MSXML2::IXMLDOMDocumentPtr docPtr;
        MSXML2::IXMLDOMNodePtr DOMNodePtr;

        // init
        TESTHR(CoInitialize(NULL)); 
        TESTHR(docPtr.CreateInstance("Msxml2.DOMDocument.6.0"));
        
        VARIANT vtTemp;

        vtTemp.vt=VT_I2;
            vtTemp.iVal = 1; //NODE_ELEMENT
        // load a document
        _variant_t varXml("c:\\Temp\\books.xml");
        _variant_t varOut((bool)TRUE);
        varOut = docPtr->load(varXml);
        if ((bool)varOut == FALSE)
            throw(0);
        MessageBox(NULL, _bstr_t(docPtr->xml), _T("Original Document"), MB_OK);
        DOMNodePtr = docPtr->createNode(vtTemp, "VIDEOS", "");
        docPtr->documentElement->appendChild(DOMNodePtr);
        MessageBox(NULL, _bstr_t(docPtr->xml), _T("New Document"), MB_OK);

    } catch(...)
    {
        MessageBox(NULL, _T("Exception occurred"), _T("Error"), MB_OK);
    }
    CoUninitialize();
}
int main()
{
    XMLDOMNodeSample();
    return 0;
}

Requirements

Implementation:

msxml3.dll, msxml2.lib (MSXML 3.0)

msxml4.dll, msxml2.lib (MSXML 4.0)

msxml5.dll, msxml2.lib (MSXML 5.0 for Microsoft Office Applications)

msxml6.dll, msxml2.lib (MSXML 6.0)

Header and IDL files: msxml2.h, msxml2.idl

Versioning

Implemented in: MSXML 2.0 and later

See Also

IXMLDOMNode Members
IXMLDOMNodeList