xml文档如下所示:
<?xml version="1.0" ?>
<message>
  <messagehead>
     <messageID>1</messageID>
     <messagetype>req</messagetype>
     <priority>1</priority>
     <resID>flow1</resID>
     <desID>flow2</desID>
     <timestamp>040330</timestamp>
  </messagehead>
  <messagebody>
     <event>
       <eventname>dialout</eventname>
       <param>callerID</param>
     </event>
  </messagebody>
</message>
我想从这个文档中读出eventname的值,并用messagebox输出。请问应该怎样写代码呢??

解决方案 »

  1.   

    有很多现成的class和lib可以用
      

  2.   

    是吗?有现成的class可以传给我吗?
      

  3.   

    getElementByTagName搜eventname,然后输出值就可以
    还可用selectsinglenode,selectnodes
      

  4.   

    Microsoft XML Core Services (MSXML) 4.0 - DOM Developer's Guide  Source: queryNodes.cpp
    This example application calls both the selectSingleNode method and the selectNodes method on an XML DOM object loaded from the stocks.xml file. The same XPath expression is input to both methods: //stock[1]/*
    This expression specifies all the child elements of the first <stock> element in the XML document. In MSXML, the selectSingleNode method returns the first element of the resultant node-set, and the selectNodes method returns all the elements in the node-set.Programmatically, this source code performs the following steps: Creates an instance of an XML DOM object (pXMLDom). 
    Calls the load method on pXMLDom to load XML data from a file (stocks.xml). 
    Calls the selectSingleNode method on pXMLDom to select a DOM node according to the input XPath expression (//stock[1]/*). Then displays the resulting node in the console if the query is successful. 
    Calls the selectNodes method on pXMLDom to select a DOM node-set according to the input XPath expression ("//stock[1]/*"). Then displays the resulting nodes in the console if the query is successful. 
    C/C++ Source File (queryNodes.cpp)#include <stdio.h>
    #include <windows.h>
    #import <msxml4.dll> raw_interfaces_only
    using namespace MSXML2;// Macro that calls a COM method returning HRESULT value:
    #define HRCALL(a, errmsg) do {     hr = (a);     if (FAILED(hr)) {         dprintf( "%s:%d  HRCALL Failed: %s\n  0x%.8x = %s\n",                 __FILE__, __LINE__, errmsg, hr, #a );         goto clean;     } } while (0)// Helper function that put output in stdout and debug window
    // in Visual Studio:
    void dprintf( char * format, ...)
    {
        static char buf[1024];
        va_list args;
        va_start( args, format );
        vsprintf( buf, format, args );
        va_end( args);
        OutputDebugStringA( buf);
        printf("%s", buf);
    }// Helper function to create a DOM instance: 
    IXMLDOMDocument * DomFromCOM()
    {
       HRESULT hr;
       IXMLDOMDocument *pxmldoc = NULL;    HRCALL( CoCreateInstance(__uuidof(DOMDocument40),
                          NULL,
                          CLSCTX_INPROC_SERVER,
                          __uuidof(IXMLDOMDocument),
                          (void**)&pxmldoc),
                "Create a new DOMDocument");    HRCALL( pxmldoc->put_async(VARIANT_FALSE),
                "should never fail");
        HRCALL( pxmldoc->put_validateOnParse(VARIANT_FALSE),
                "should never fail");
        HRCALL( pxmldoc->put_resolveExternals(VARIANT_FALSE),
                "should never fail");   return pxmldoc;
    clean:
       if (pxmldoc)
        {
          pxmldoc->Release();
        }
       return NULL;
    }VARIANT VariantString(BSTR str)
    {
       VARIANT var;
       VariantInit(&var);
       V_BSTR(&var) = SysAllocString(str);
       V_VT(&var) = VT_BSTR;
       return var;
    }void ReportParseError(IXMLDOMDocument *pDom, char *desc) {
       IXMLDOMParseError *pXMLErr=NULL;
       BSTR bstrReason = NULL;
       HRESULT hr;
       HRCALL(pDom->get_parseError(&pXMLErr),
                "dom->get_parseError: ");
       HRCALL(pXMLErr->get_reason(&bstrReason),
                "parseError->get_reason: ");
       
       dprintf("%s %S\n",desc, bstrReason);
    clean:
       if (pXMLErr) pXMLErr->Release();
       if (bstrReason) SysFreeString(bstrReason);
    }int main(int argc, char* argv[])
    {
       IXMLDOMDocument *pXMLDom=NULL;
       IXMLDOMNodeList *pNodes=NULL;
       IXMLDOMNode *pNode=NULL;
       BSTR bstr = NULL;
       VARIANT_BOOL status;
       VARIANT var;
       HRESULT hr;
       long length;   CoInitialize(NULL);   pXMLDom = DomFromCOM();
       if (!pXMLDom) goto clean;   VariantInit(&var);
       var = VariantString(L"stocks.xml");
       HRCALL(pXMLDom->load(var, &status), "dom->load(): ");   if (status!=VARIANT_TRUE) {
          ReportParseError(pXMLDom, 
             "Failed to load DOM from stocks.xml");
          goto clean;
       }   // Query a single node.
       if (bstr) SysFreeString(bstr);
       bstr = SysAllocString(L"//stock[1]/*");
       HRCALL(pXMLDom->selectSingleNode(bstr, &pNode),
          "dom->selectSingleNode: ");
       if (!pNode) {
          ReportParseError(pXMLDom, "Calling selectSingleNode ");
       }
       else {
          dprintf("Result from selectSingleNode:\n");
          if (bstr) SysFreeString(bstr);
          HRCALL(pNode->get_nodeName(&bstr)," get_nodeName ");
          dprintf("Node, <%S>:\n", bstr);
          if (bstr) SysFreeString(bstr);
          HRCALL(pNode->get_xml(&bstr), "get_xml: ");
          dprintf("\t%S\n\n", bstr);
       }   // Query a node-set.
       if (bstr) SysFreeString(bstr);
       bstr = SysAllocString(L"//stock[1]/*");
       HRCALL(pXMLDom->selectNodes(bstr, &pNodes), "selectNodes ");
       if (!pNodes) {
          ReportParseError(pXMLDom, "Error while calling selectNodes ");
       }
       else {
          dprintf("Results from selectNodes:\n");
          HRCALL(pNodes->get_length(&length), "get_length: ");
          for (long i=0; i<length; i++) {
             if (pNode) pNode->Release();
             HRCALL(pNodes->get_item(i, &pNode), "get_item: ");
             if (bstr) SysFreeString(bstr);
             HRCALL(pNode->get_nodeName(&bstr), "get_nodeName: ");
             dprintf("Node (%d), <%S>:\n",i, bstr);
             SysFreeString(bstr);
             HRCALL(pNode->get_xml(&bstr), "get_xml: ");
             dprintf("\t%S\n", bstr);
          }
       }clean:
       if (bstr) SysFreeString(bstr);
       if (&var) VariantClear(&var);
       if (pXMLDom) pXMLDom->Release();
       if (pNodes) pNodes->Release();
       if (pNode) pNode->Release();   CoUninitialize();
       return 0;
    }
    To add queryNodes.cpp to the project Select FileView on the project browser, and highlight queryNodeProj files. From the File menu, select New. 
    On the Files tab of the New dialog box, highlight C++ Source File. Then type "queryNodes.cpp" in the File name text box, and click OK. 
    Copy the C/C++ source code above and paste it into the source file you just created. 
    Next, we'll add the resource file to the queryNodes project.
      

  5.   

    Microsoft XML Core Services (MSXML) 4.0 - DOM Developer's Guide  Resource: stocks.xml
    XML File (stocks.xml)<?xml version="1.0"?>
    <?xml:stylesheet type="text/xsl" href="stock.xsl"?>
    <portfolio xmlns:dt="urn:schemas-microsoft-com:datatypes">
      <stock exchange="nasdaq">
        <name>new</name>
        <symbol>zzzz</symbol>
        <price dt:dt="number">20.313</price>
      </stock>
      <stock exchange="nyse">
        <name>zacx corp</name>
        <symbol>ZCXM</symbol>
        <price dt:dt="number">28.875</price>
      </stock>
      <stock exchange="nasdaq">
        <name>zaffymat inc</name>
        <symbol>ZFFX</symbol>
        <price dt:dt="number">92.250</price>
      </stock>
      <stock exchange="nasdaq">
        <name>zysmergy inc</name>
        <symbol>ZYSZ</symbol>
        <price dt:dt="number">20.313</price>
      </stock>
    </portfolio>
    To add stocks.xml to the project Create a new C++ source file. For detailed instructions on how to do this, see Set Up My Visual C++ Project. Name the new file queryNodes. 
    Copy stocks.xml, above, and paste it into the source file you just created. 
    Next, build and run the queryNodes project. The result should be the output shown in the following topic.
      

  6.   

    Microsoft XML Core Services (MSXML) 4.0 - DOM Developer's Guide  Output
    After you build and run the application, you should see the following output displayed in the application's console.Result from selectSingleNode:
    Node, <name>:
            <name>new</name>Results from selectNodes:
    Node (0), <name>:
            <name>new</name>
    Node (1), <symbol>:
            <symbol>zzzz</symbol>
    Node (2), <price>:
            <price xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="number">20.313</price>
    Notice that the output above confirms that the selectSingleNode method picks out only the first node from the node set as specified by the XPath expression (//stock[1]/*). The selectNodes method returns the entire set of nodes specified by this XPath expression.Next, we'll demonstrate how to manage object lifetime using reference counting.
      

  7.   

    <?xml version="1.0" ?>
    <message>
      <messagehead>
         <messageID>1</messageID>
         <messagetype>req</messagetype>
         <priority>1</priority>
         <resID>flow1</resID>
         <desID>flow2</desID>
         <timestamp>040330</timestamp>
      </messagehead>
      <messagebody>
         <event>
           <eventname>dialout</eventname>
           <param>callerID</param>
         </event>
      </messagebody>
    </message>
    利用xpath="message/messagebody/eventname" 得到这个节点,然后取值就可以了
    试试看这个函数
    BOOL ReadXML(MSXML2::IXMLDOMDocument2Ptr pDoc, CString xpath, CString *nodevalue)
    {
    int ret = 0; MSXML2::IXMLDOMNodeListPtr    pNodeList = NULL;  
    MSXML2::IXMLDOMNodePtr   pNode = NULL;   try
    {
    //找到节点
    pNode = (pDoc)->selectSingleNode(_bstr_t(xpath));
    //得到节点得值
    nodevalue->AppendFormat(_T("%s"),CA2T((char *)pNode->Gettext()));
    }
    catch (...)
    {
    ret = 2;
    return ret; //出错
    } return ret;}
      

  8.   

    呵呵,上面太乱了,就是使用xpath把那个节点找到就行了
      

  9.   

    http://www.kan68.com/index3.htm?user=powrman