Thinking about XML file processing

xiaoxiao2021-03-06  52

Thoughts on XML Document Processing [10:18 on May 11, 2004] 1. The node path information (BCB) is indicated by a string separated by a separator. Thinking reason, some simple parameters, if the nesting is more in-depth, if you use a general XML process, you will gradually, you need to define some variables, too much trouble.

Suppose the XML file is Ohahu 6800 50

The way to access the port child node of XNetConfig needs to get the XNetConfig node before getting the PORT node. The following is obtained directly through the string "xnetconfig.port" to directly obtain the PORT node.

The function is implemented as follows: // Defines a callback function template. In this way, do not care about all the operations of finding a node, as long as you want to do this node?

// Function return value type BOOL * Representation is a pointer actionProc name parameter _di_ixmlnode pnode, void * pValue (void * pvalue is adaptive) typef Bool (_ DI_IXMLNode PNode, void * pvalue);

// Ansistring and CString are similar

Bool RetriEvenodeBydir (_di_ixmlnode pparent, ansistring strlist, void * pvalue, actionproc doit) {

Try

{

_DI_ixmlnodelist childlist = pParent-> childnodes;

if (Childlist == Null || strlist == null)

Return false; // node does not exist, natural treatment is unsuccessful

Else

{

_di_ixmlnode childnode;

INT NLEN = Strlist.ansipos (".")> 0? strlist.ansipos (".") - 1: strlist.length ();

Ansistring strcur = strlist.substring (1, nlen); // This local test is not sufficient enough

IF ((ChildNode = childlist-> FindNode (WideString (strcur))))! = null)

{IF (strcur == strlist)

{

Return DOIT (CHILDNODE, PVALUE); // Call Processing Function

}

Else

{

Int nStart = strlist.ansipos (".")> 0? strlist.ansipos (".") 1: 1;

Strcur = strlist.substring (nStart, strlist.length ());

IF (getXmlminnodeBydir (ChildNode, Strcur, PValue, DOIT) // Removable call, processes sub-nodes

Return True;

}

}

}

}

Catch (...)

{

Return False;

}

Return False;} This function can perform the setting node value, attribute, get node value, attribute, and other operations via ActionProc.

ActionProc example

-------------------------------------------------- --------------------------------------------2. Find nodes according to the node name

As the PORT node above, only one "port" is needed as a parameter without a full path. Of course, this will not be recognized if there are multiple ports of the entire document, but can be modified, so that this function can support all the qualified nodes. If you can join the callback function as above, it is better. Function is as follows: bool GetTextIntByTag (_di_IXMLNode pParent, AnsiString strTag, int & nValue) {// bool bSuccess = false; _di_IXMLNodeList ChildList = pParent-> ChildNodes; if (ChildList == NULL || strTag == NULL) return false; else {_di_IXMLNode ChildNode; if (!! (ChildNode = ChildList-> FindNode (WideString (strTag))) = 0 && ChildNode-> ChildNodes = NULL && ChildNode-> ChildNodes-> Count == 1) {int nOldValue = nValue; try {AnsiString str; str = ChildNode-> text; nvalue = str.Toint (); return true;} catch (...) {nvalue = noldvalue;}} else {for (int i = 0; icount; i ) {childnode = childlist-> GetNode (i); if (getXmlTextINTBYTAG (CHILDNODE, STRTAG, NVALUE)) Return True;}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}

-------------------------------------------------- ------------------------------------------ 3. As can be seen from the above, the use of the callback function in the traversal function is a very good choice. Of course, a good callback function is to be defined, this is the advantage of C , such as the above Void pointer, we can convert the VoID pointer to the required type. The conversion of the pointer, in addition to the direct conversion below, Dynamic_cast, static_cast, const_cast, where static_cast is more, specific or find information yourself. To make a program, you can't find information.

The first function of the callback function example: // Get Node Properties Bool getNodeStr (_di_ixmlnode pnode, void * pvalue) {ANSISTRING STROLDVALUE = * (ANSISTRING *) PVALUE; // Convert to string pointer, then get this object try {/ / Put the information of the node in pValue, the following assignment personal feels that there is no problem, because the Ansistring is defined outside, then follow the Ansistring rules, of course, if the PVALUE is a pointer to the int, I don't know what will happen. * (ANSISTRING *) PVALUE = pnode-> text; return true;} catch (...) {* (Ansistring *) pValue = StroldValue;} Return False;} // Set node Attribute Bool setNodeStr (_di_ixmlnode pnode, void * pvalue) {try {// Set the value of the node, _di_ixmlnode is a COM interface, so here you want to use wideString pnode-> text = WideString (* (Ansistring *) PVALUE); Return True;} catch (...) {returnaf false;}}

Example: // DOCNODE is an interface (pointer to the XML document node (pointer), for the above, the xnetconfig node int nvalue = left; retrievenodeBydir (DOCNODE, ANSISTRING ("Mainform.Left"), (void *) & nvalue, SetNodeInt); if (RetrieveNodeByDir (DocNode, AnsiString ( "MainForm.Left"), (void *) & nValue, GetNodeInt)) Left = nValue; with examples of AnsiString AnsiString = Caption strValue; RetrieveNodeByDir (DocNode, AnsiString ( "MainForm.Caption "), (void *) & strvalue, setNodeStr); if (DOCNODEBYDIR (DOCNODE, ANSISTRING (" Mainform.caption "), (void *) & strvalue, getnodeStr)) CAPTION = Strvalue; maybe someone does not understand the meaning of the callback function is What, by the way. A relatively shallow application is to put the above 1 (including typedef), in fact, it can be packaged into the DLL file (not considering thread problems here), so as long as the implementation is specified in the EXE. Function, such as the value above, read value, etc., you can use it, you don't have to care, how to find the implementation details in accordance with the path to "xxx.aa". Moreover, for different functions, such retrieval path code, just write one time. It should be noted that ActionProc must be a fixed structure (parameter, return value), which may be different data types, but accounting for the number of bytes must be the same. -------------------------------------------------- -------------------------------------------- 4. Top mentioned _ Di_ixmlnode is an interface, and COM has a much different law. Of course, there is a different implementation process in VC. Here I think I think there is necessary to say some of the XML COM calls. #import "c: // windows //system32/msxml.dll "named_guids Add this in stdafx.h, introduced XML calls, related to the application // Application Summary //Http://bbs.hziee.edu.cn/bbscon. PHP? Board = VC & ID = 239

In Many of Microsoft, the use of GET__NEWENUM, knowing his way of use, and know how to traverse specific, please post the code of the traversal node, because you have the mechanism of COM There is no big grasp, let's talk about it, don't throw stones, huh, huh, huh, / 还 tDef void (* enumnodeproc) (MSXML :: ixmldomnodeptr start);

INT CXmlFile :: EnumNode (MSXML :: IXMLDOMNodeListPtr pNodeList, EnumNodeProc EnumProc) {if (pNodeList == NULL) return 0; MSXML :: IXMLDOMNodePtr pNode; CComPtr spDispatch; IUnknownPtr pUnk; int nCnt = 0; try {HRESULT hr = pNodeList- > GET__NEWENUM (& PUNK); // Generates PUNK to COM's interface calling if (Failed (HR)) {Return NCNT;} ccomptr penum; // But PUNU is not an ENUM interface, but the Enum interface is already included in the Punk. , / / ​​Is not important here that the PUNKNOWNPTR interface is not important. It is important that it wants to include the INUM interface // However, the front_newenum needs to be an iUnknown interface. HR = punk-> queryinterface (IID_IENUMVARIANT, (Void **) & penum); // Generate IEnum call If (penum) {penum-> reset (); ulong fget = 1; while (succeededed (hr) && fget> 0) { _VARIANT_T VARDISP; HR = Penum-> Next (1, & VARDISP, & FGET); // Next Record IF (Succeeded (HR) && Fget> 0) {PNODE = VARDISP.PDISPVAL; NCNT ; EnumProc (PNode); // Call The callback function pnode = null;}} penum.release (); // If the call needs to be released, the COM is accurate, in order to automatically release when there is no call}, (_COM_ERROR & E) { } return ncnt;} About the use of GET_NEWENUM Which is trying it out, and I will inform. The process of COM has a better introduction in the electronic version of the VC Technology Inside the VC Technology (looking for it online). June 26 2:43 to make up: Keywords: String positioning XML node, XML insertion node

I found that the MSXML itself took the function, sweat

reference:

Use as follows: MSXML :: IXMLDOMDocumentPtr m_pDoc; MSXML :: IXMLDOMNodePtr NodePtr = NULL; if (! (NodePtr = m_pDoc-> selectSingleNode (_bstr_t ( "distributeservice / mainservice"))) = NULL) printf ( "Find distributeservice / mainservice / N ");

Then there is a need to add a node to create Xmldocument, then insert it by the child node, such as

转载请注明原文地址:https://www.9cbs.com/read-83100.html

New Post(0)