Thoughts on XML Document Processing [10:18, May 11, 2004]

zhaozj2021-02-16  70

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 the node, as long as you do what you want to do / / function return value type BOOL * Representation is a pointer ActionProc name parameter _di_ixmlnode pnode, void * pValue (void * pvalue is adaptive) typedef bool (* ActionProc) (_ di_IXMLNode pNode, void * pValue); // AnsiString CString and 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 processing is unsuccessful ELSE {_di_ixmlnode childNode; int Nlen = strlist.ansipos (".")> 0? strlist.ansipos (".") - 1: strlist.length (); Ansistring strcur = strlist.substring (1, nlen); // This place test is not enough if ((ChildNode = childlist-> FindNode (WideString (strcur)))! = NULL) {if (strcur == strlist) {Return DOIT (CHILDE, 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 subtitudes 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. There is an example of ACTIONPROC --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------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. The 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 I don't think there is any problem, because in the outside / / definition is ASISTRING, then follow the Ansistring rules, of course, if the external definition PVALUE // is a pointer to the int, I don't know what will happen * (ANSISSTRING *) PVALUE = pnode-> text; return true;} catch (...) {* (ANSISTRING *) PVALUE = StroldValue;} Return False;} // Set Node Properties Bool SetNodeStr (_di_ixmlnode pnode, void * pvalue {Try {// Set the value of the node, here _di_ixmlnode is a COM interface, so here should use wideString pnode-> text = WideString (* (ANSISTRING *) pValue; return true;} catch (...) { Use example:}}

// DOCNode is the interface (pointer to the XML document node (pointer ??), which is the xnetconfig node int nvalue = left; retrievenodeBydir (DocNode, Ansistring ("Mainform.Left"), (void *) & nvalue, setNodei NT) ; if (RetrieveNodeByDir (DocNode, AnsiString ( "MainForm.Left"), (void *) & nValue, GetNod eInt)) Left = nValue; AnsiString strValue // example with an AnsiString = Caption; RetrieveNodeByDir (DocNode, AnsiString ( "MainForm. Caption "), (void *) & strval, set nodestr); if (retrievenodeBydir (DOCNODE, ANSISTRING (" Mainform.caption ", (Void *) & strvalue, g etc = strestr) CAPTION = STRVALUE

Maybe some people don't understand what is the meaning of the callback function, 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 COMs in Microsoft, GET__NEWENUM usually appeared in many COMs, knowing his way of use, and knowing how to traverse specific, please post the code of the traversal node, because I don't have a big grasp of my mechanism to COM. I am wrong below, but I don't want to throw stones, huh, huh.

// callback function is a typedef void (* EnumNodeProc) (MSXML :: IXMLDOMNodePtr pElement); 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); // Generate PUNK to COM interface call IF (Failed (HR)) {Return NCNT;} ccomptr Penum; // But PUNK is not an enum interface, but the Enum interface is already included in the PUNK, // Is the PUNKNOWNPTR interface is not important, it is important that it wants to include the INUM interface // But, front GET_NEWENUM needs 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's count is accurate, and it can be automatically released when there is no call.} Catch (_ELROR & E) {;} return ncnt;}

About GET_NEWENUM's usage, from the 9CBS Forum VC version of the old big brother masterz guidance, there is also a member _newenum used, I have not tried it, refer to the above method, always mistake, if there is any trial, Troublem. 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: http://www.vcer.net/showtip.jsp?tipid=2248

The method is 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

MSXML :: IXMLDOMDocumentPtr pDoc; MSXML :: IXMLDOMElementPtr pElem = NULL; pDoc.CreateInstance (__ uuidof (MSXML :: DOMDocument)); pDoc-> loadXML (_bstr_t ( "hello")); pElem = pDoc-> GetdocumentElement (); if (pelem! = null) {_BSTR_T TMP = pelem-> getXML (); pelement-> appendchild ((msxml :: ixmldomnodeptr) pelem); // Discover not, then} pdoc.release (); pdoc = null;

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

New Post(0)