¼òµ¥µÄXMLÎļþ¶Áд £¨×ª£©
×¼±¸
¿ª·¢¹¤¾ß£ºVS2005£¬
Ç°Ì᣺°²×°msxml6½âÎöÆ÷£¬msÍøÕ¾¿ÉÏÂÔØ
ͨ¹ýÏòµ¼´´½¨VC++ MFC ¹¤³Ì£¬»ùÓÚ¶Ô»°¿ò
Ìí¼ÓÒýÓÃ:
//ÔÚStdAfx.hÖÐÒýÈ붯̬Á´½Ó¿â MSXML.DLL(C:\windows\system32\msxml6.dll)
#import <msxml6.dll>
±à¼×ÊÔ´£¬Ìí¼Ó¿Ø¼þ
ÔÚbookid¶ÔÓ¦µÄÎı¾¿òÓÒ¼ü£¬Ìí¼Ó±äÁ¿°ó¶¨£¨ÈçÏÂͼ£¬°ó¶¨¿Ø¼þÖµ£¬±äÁ¿ÃûΪ:m_strId)
¼ÌÐø¸øÁíÍâÁ½¸öÎı¾¿òÒ²°ó¶¨±äÁ¿£¬·Ö±ðΪ£ºm_strAuthor£¬m_strTitle
¶¨ÒåÈ«¾Ö±äÁ¿
MSXML2::IXMLDOMDocumentPtr pDoc;//xmlÎĵµÖ¸Õë
long bookIndex,bookCount ;//xmlÄÚÈÝÖУ¬µ±Ç°Ë÷ÒýÓëbookÊýÁ¿£¬ÓÃÓÚÏÔʾ”ÉÏÒ»Ìõ¼Ç¼”ºÍ”ÏÂÒ»Ìõ¼Ç¼”
_bstr_t xmlfile;//xmlÎĵµÂ·¾¶
×¢²áʼþ
Ë«»÷¸÷¸ö°´¼ü£¬Ìí¼Óʼþ°ó¶¨
дÈëxml·½·¨£º
void CMFC2005Dlg::OnBnClickedButtonWritexml()
{
UpdateData();
if (pDoc!=NULL) pDoc=NULL;
//´´½¨DOMDocument¶ÔÏó
HRESULT hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument60));
if(!SUCCEEDED(hr))
{
MessageBox( _T("ÎÞ·¨´´½¨DOMDocument¶ÔÏó£¬Çë¼ì²éÊÇ·ñ°²×°ÁËMS XML Parser ÔËÐпâ!"));
return ;
}
MSXML2::IXMLDOMElementPtr xmlRoot ;
//¸ù½ÚµãµÄÃû³ÆΪBooks
xmlRoot=pDoc->createElement((_bstr_t)"Books");
//´´½¨¸öbook½Úµã
for (int i=0;i<10;i++)
{
//´´½¨bookÔªËز¢Ìí¼Óµ½booksÖÐ
MSXML2::IXMLDOMElementPtr xmlBootNode ;
xmlBootNode=pDoc->createElement((_bstr_t)"Book");
//ÉèÖÃÊôÐÔ
CString idvalue;
idvalue.Format(_T("%s%d"), m_strId, i);
xmlBootNode->setAttribute("id",(_bstr_t)idvalue);
//ϼ¶½áµã
MSXML2::IXMLDOMElementPtr pNode;
//Ìí¼Ó“author”ÔªËØ
CString authorValue ;
authorValue.Format(_T("%s%d"),m_strAuthor,i);
pNode=pDoc->createElement((_bstr_t)"Author");
pNode->Puttext((_bstr_t)authorValue);
xmlBootNode->appendChild(pNode);
//Ìí¼Ó“Title”ÔªËØ
CString titleValue ;
titleValue.Format(_T("%s%d"),m_strTitle,i);
pNode=pDoc->creat
Ïà¹ØÎĵµ£º
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
//sqlserverÉí·ÝÑéÖ¤
//s ......
¼ÓÔØÍⲿµÄXMLÎļþ£¬ÎļþÃûÊÇ1.xml;
<?xml version="1.0" encoding="utf-8"?>
<website>
<name a="Ãû×Ö1">ÖÜС´¨1</name>
<name a="Ãû×Ö2">ÖÜС´¨2</name>
<name a="Ãû×Ö3">ÖÜС´¨3</name>
<name a="Ãû×Ö4">ÖÜС´¨4</name>
<name a="Ãû×Ö5"> ......
XML CDATA Îı¾
ËùÓÐ XML ÎĵµÖеÄÎı¾¾ù»á±»½âÎöÆ÷½âÎö¡£
Ö»ÓÐ CDATA Çø¶Î£¨CDATA section£©ÖеÄÎı¾»á±»½âÎöÆ÷ºöÂÔ¡£
Parsed Data
XML ½âÎöÆ÷ͨ³£»á½âÎö XML ÎĵµÖÐËùÓеÄÎı¾¡£
µ±Ä³¸ö XML ÔªËر»½âÎöʱ£¬Æä±êÇ©Ö®¼äµÄÎı¾Ò²»á±»½âÎö£º
<message>´ËÎı¾Ò²»á±»½âÎö</message>
½âÎöÆ÷Ö®ËùÒÔÕâô×öÊÇÒòÎ ......
ÔÚXMLÎĵµÖеÄËùÓÐÎı¾¶¼»á±»½âÎöÆ÷½âÎö¡£
Ö»ÓÐÔÚCDATA²¿¼þÖ®ÄÚµÄÎı¾»á±»½âÎöÆ÷ºöÂÔ¡£
½âÎöÊý¾Ý
XML ½âÎöÆ÷ͨ³£Çé¿öÏ»ᴦÀíXMLÎĵµÖеÄËùÓÐÎı¾¡£
µ±XMLÔªËر»½âÎöµÄʱºò£¬XMLÔªËØÄÚ²¿µÄÎı¾Ò²»á±»½âÎö:
<message>This text is also parsed</message>
XML½âÎöÆ÷ÕâÑù×öµÄÔÒòÊÇXMLÔªËØÄÚ²¿¿ÉÄÜ» ......
дUnmanaged CodeÔÚ.NETʱ´ú³ÉΪһÖֺܱ¯²ÒµÄÊ£¬µ±ÄãÐèÒª´¦ÀíXMLÎļþʱ£¬ÕâÖָоõ»á±äµÃÓÈÆäÇ¿ÁÒ¡£FCLÖеÄSystem.XML¶à¼òµ¥°¡£¬Á¬Steve Ballmer¶¼ÖªµÀÔõôÓá£
ÊÂÇé²»»á×ÜÊÇÄÇôÀíÏëµÄ£¬¼ÙÈçÄãÒªÔÚC/C++³ÌÐòÀï´¦ÀíXMLÔõô°ìÄØ£¿
Ñ¡ÔñÒ»£ºÊÐÃæÉϵÄXML lib»¹ÊÇÓм¸¸öµÄ£¬×îÓÐÃûµÄµ±È»ÊÇlibXML¡£ÎÒÒ»ÄêÇ°Óùý£¬ºÜ²»´í£¬ÎÒ»¹ ......