Parse strings in XML format based on COM in C ++ to generate

Source: Internet
Author: User

Currently, strings in XML format are widely used in the interaction of various information. Whether you are engaged in network development, desktop application development, or even game development, you will inevitably encounter strings in XML format, this article introduces how to parse and generate xml strings Based on COM in C ++.

First, we need to clarify two basic concepts: COM and XML.

First, the full name of COM is the component object model, component object model, and OMG (objectmanagementgroup) divides complex applications into components with a single function and a small scale, the Common Object Request breaker architecture standard is released for more and more complex program functions. This standard is executed in UNIX, and in windows, this standard is com. For more information, see http://blog.csdn.net/crich_moon/article/details/6614392.

Let's take a brief look at XML. XML (Extensible Markup Language) can be used to expand the markup language. Like HTML, It is SGML (Standard Generalized Markup Language, standard General Markup Language ). XML is cross-platform in the Internet environment and relies on content technology. It is a powerful tool for processing structured document information. XML is a simple data storage language that uses a series of simple tags to describe data. These tags can be conveniently created, although XML occupies more space than binary data, XML is extremely easy to master and use.

Next, let's take a look at how to use the COM component to parse strings in XML format.

First, you must include the header file.

# Include <windows. h>
# Include <MSXML. h>

Then define the class in the header file and declare the member functions of the class:

Class xmlhelper
{
Public:
Xmlhelper (void );
Xmlhelper (lpctstr xmlfilename );
~ Xmlhelper (void );

Hresult readitemvalue (lpctstr stritem, lpctstr strvaluename, tchar * pchvalue, DWORD dwcount );
Hresult readitemattributevalue (maid );
Hresult writeitemvalue (maid );
PRIVATE:
BSTR m_szxmlfile;
BSTR m_szxmlstr;
Int m_nxmllen;
};

Then define the implementation of member functions in the CPP file:

Xmlhelper: xmlhelper (lpctstr xmlfilename)
{
If (! Xmlfilename)
{
Assert (0 );
}
Else
{
M_szxmlfile = sysallocstring (xmlfilename );
}
}
Hresult xmlhelper: readitemvalue (lpctstr stritem, lpctstr strvaluename, tchar * pchvalue, DWORD dwcount)
{
Hresult hR = e_fail;
BSTR ptext = NULL;

Ixmldomdocument * pixmldoc = NULL;
Ixmldomelement * proot = NULL;

Cocreateinstance (clsid_domdocument, null, clsctx_inproc_server, iid_ixmldomdocument, (lpvoid *) & pixmldoc );

If (pixmldoc)
{
Pixmldoc-> put_async (variant_false );

Variant vxmlfile;
Vxmlfile. Vt = vt_bstr;
Vxmlfile. bstrval = (BSTR) m_szxmlfile;

Variant_bool Vb;
Pixmldoc-> load (vxmlfile, & VB );
If (VB)
{
Pixmldoc-> get_documentelement (& proot );

If (proot)
{
Ixmldomnode * pmainnode = NULL;
Ixmldomelement * pmainnodeele = NULL;

Proot-> selectsinglenode (BSTR) stritem, & pmainnode );
If (pmainnode)
{
Pmainnode-> QueryInterface (iid_ixmldomelement, (void **) & pmainnodeele );
Pmainnode-> release ();
}

If (pmainnodeele)
{

Ixmldomnode * pvaluenode = NULL;
Ixmldomelement * pvaluenodeele = NULL;

Pmainnodeele-> selectsinglenode (BSTR) strvaluename, & pvaluenode );
If (pvaluenode)
{
Pvaluenode-> QueryInterface (iid_ixmldomelement, (void **) & pvaluenodeele );
Pvaluenode-> release ();
}
If (pvaluenodeele)
{
HR = pvaluenodeele-> get_text (& ptext );
Pvaluenodeele-> release ();
}
Pmainnodeele-> release ();
}
Proot-> release ();
}
}

Pixmldoc-> release ();
}

If (ptext)
{
Wcsncpy (pchvalue, ptext, dwcount );
Sysfreestring (ptext );
}
Return hr;

}

Hresult xmlhelper: readitemattributevalue (maid, maid, maid)
{
Waitforsingleobject (m_hsyncevent, 5000 );
Hresult hR = e_fail;
Variant vattri = {0 };

Ixmldomdocument * pixmldoc = NULL;
Ixmldomelement * proot = NULL;

Cocreateinstance (clsid_domdocument, null, clsctx_inproc_server, iid_ixmldomdocument, (lpvoid *) & pixmldoc );

If (pixmldoc)
{
Pixmldoc-> put_async (variant_false );

Variant vxmlfile;
Vxmlfile. Vt = vt_bstr;
Vxmlfile. bstrval = (BSTR) m_szxmlfile;

Variant_bool Vb;
Pixmldoc-> load (vxmlfile, & VB );
If (VB)
{
Pixmldoc-> get_documentelement (& proot );

If (proot)
{
Ixmldomnode * pmainnode = NULL;
Ixmldomelement * pmainnodeele = NULL;

Proot-> selectsinglenode (BSTR) stritem, & pmainnode );
If (pmainnode)
{
Pmainnode-> QueryInterface (iid_ixmldomelement, (void **) & pmainnodeele );
Pmainnode-> release ();
}

If (pmainnodeele)
{

Ixmldomnode * pvaluenode = NULL;
Ixmldomelement * pvaluenodeele = NULL;

Pmainnodeele-> selectsinglenode (BSTR) strvaluename, & pvaluenode );
If (pvaluenode)
{
Pvaluenode-> QueryInterface (iid_ixmldomelement, (void **) & pvaluenodeele );
Pvaluenode-> release ();
}
If (pvaluenodeele)
{
// HR = pvaluenodeele-> get_text (& ptext );

BSTR bstrattr = sysallocstring (strattributename );
HR = pvaluenodeele-> getattribute (bstrattr, & vattri );
Sysfreestring (bstrattr );

Pvaluenodeele-> release ();
}
Pmainnodeele-> release ();
}
Proot-> release ();
}
}

Pixmldoc-> release ();
}

If (vattri. Vt = vt_bstr & vattri. bstrval)
{
Wcsncpy (strattributevalue, vattri. bstrval, dwcount );
Sysfreestring (vattri. bstrval );
}

Setevent (m_hsyncevent );
Return hr;
}

Hresult xmlhelper: writeitemvalue (maid)
{
Waitforsingleobject (m_hsyncevent, 5000 );
Hresult hR = e_fail;
Ixmldomdocument * pixmldoc = NULL;
Ixmldomelement * proot = NULL;

Cocreateinstance (clsid_domdocument, null, clsctx_inproc_server, iid_ixmldomdocument, (lpvoid *) & pixmldoc );

If (pixmldoc)
{
Pixmldoc-> put_async (variant_false );

Variant vxmlfile;
Vxmlfile. Vt = vt_bstr;
Vxmlfile. bstrval = (BSTR) m_szxmlfile;

Variant_bool Vb;
Pixmldoc-> load (vxmlfile, & VB );
If (! VB)
{
Pixmldoc-> createelement (BSTR) strrootname, & proot );
If (proot)
{
Pixmldoc-> appendchild (proot, null );
}
}

If (! Proot)
{
Pixmldoc-> get_documentelement (& proot );
}
If (proot)
{

Ixmldomnode * pmainnode = NULL;
Ixmldomelement * pmainnodeele = NULL;

Proot-> selectsinglenode (BSTR) stritem, & pmainnode );
If (! Pmainnode)
{
Pixmldoc-> createelement (BSTR) stritem, & pmainnodeele );
Proot-> appendchild (pmainnodeele, null );
}
Else
{
Pmainnode-> QueryInterface (iid_ixmldomelement, (void **) & pmainnodeele );
Pmainnode-> release ();
}

If (pmainnodeele)
{

Ixmldomnode * pvaluenode = NULL;
Ixmldomelement * pvaluenodeele = NULL;

Pmainnodeele-> selectsinglenode (BSTR) strvaluename, & pvaluenode );
If (! Pvaluenode)
{
Pixmldoc-> createelement (BSTR) strvaluename, & pvaluenodeele );
Pmainnodeele-> appendchild (pvaluenodeele, null );
}
Else
{
Pvaluenode-> QueryInterface (iid_ixmldomelement, (void **) & pvaluenodeele );
Pvaluenode-> release ();
}
If (pvaluenodeele)
{
Variant VUT;
Vut. Vt = vt_bstr;
Vut. bstrval = sysallocstring (strattributevalue );

Pvaluenodeele-> setattribute (BSTR) strattributename, VUT );

Sysfreestring (VUT. bstrval );

BSTR bstrvalue = sysallocstring (strvalue );
HR = pvaluenodeele-> put_text (bstrvalue );
Sysfreestring (bstrvalue );
Pvaluenodeele-> release ();
}
Pmainnodeele-> release ();
}
Proot-> release ();
}

Pixmldoc-> Save (vxmlfile );
Pixmldoc-> release ();
}

Setevent (m_hsyncevent );
Return hr;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.