|
To get specific group:
CXmlNodeWrapper Group(node.FindNode("//Group[text() = 'Epson POS Printers'"));
This will get the group "Epson POS Printers"
To get specific printer:
CXmlNodeWrapper Printer(node.FindNode("//Printer[text() = 'FX-870'"));
This will get the FX-870 printer.
Since all the printer properties are defined as Nodes rather than attributes,
You have to use GetNode function to get a specific node, and you cant use GetValue function.
Example:
CXmlNodeWrapper node;
node = Printer.GetNode("Lang");
CString szLang = node.Text();
Creative minds - create creative creations!
|
|
|
|
|
Hi Alex, Thanks for the info. In my case, the example to get a specific group such as:
CXmlNodeWrapper Group(node.FindNode("//Group[text() = 'Epson POS Printers'"));
will not work for me as I am defining the groups and printers in the XML file. In other words, I do not know the names to search for until I've loaded and parsed the XML file. The XML file defines printer types, names, and properties in my application. Anyway, here is the way I ended up doing it and it worked fine for me...
void CPrinterFileInfo::ParsePrinterDefinesNode(IDispatch *pNode)
{
CGroupEntryInfo *gei;
CXmlNodeWrapper node(pNode);
CXmlNodeListWrapper GroupList(node.FindNodes(_T("//Group"))); // Thermal, SIDM, etc...
GroupList.Start();
CXmlNodeWrapper List2(GroupList.Next());
while(List2.IsValid())
{
gei = new CGroupEntryInfo;
ParseGroupDefinesNode(List2, *(gei));
AddGroupEntry(*(gei));
List2 = GroupList.Next();
delete gei;
}
}
void CPrinterFileInfo::ParseGroupDefinesNode(CXmlNodeWrapper& node, CGroupEntryInfo& gei)
{
CDeviceEntryInfo *dei;
gei.SetGroupName(node.GetAttribVal(XML_GROUP_NAME));
gei.SetGroupDescription(node.GetAttribVal(XML_GROUP_DESC));
for(int j= 0; j < node.NumNodes(); j++)
{
dei = new CDeviceEntryInfo;
CXmlNodeWrapper List(node.GetNode(j));
ParseMemberDefinesNode(List, *(dei));
gei.AddMemberEntry(*(dei));
delete dei;
}
}
void CPrinterFileInfo::ParseMemberDefinesNode(CXmlNodeWrapper& node, CDeviceEntryInfo& dei)
{
dei.SetDeviceName(node.GetAttribVal(XML_DEVICE_NAME));
dei.SetDeviceClass(node.GetAttribVal(XML_DEVICE_CLASS));
dei.SetDeviceFeatures(node.GetAttribVal(XML_DEVICE_FEATURES));
dei.SetDevicePaperPaths(node.GetAttribVal(XML_DEVICE_PAPER));
dei.SetDeviceFonts(node.GetAttribVal(XML_DEVICE_FONTS));
dei.SetDeviceColumns(node.GetAttribVal(XML_DEVICE_COLS));
dei.SetDeviceInterface(node.GetAttribVal(XML_DEVICE_INTERFACES));
dei.SetDeviceLanguage(node.GetAttribVal(XML_DEVICE_LANG));
dei.SetDeviceExFeatures(node.GetAttribVal(XML_DEVICE_EXFEATURES));
dei.SetDeviceBuffer(node.GetAttribVal(XML_DEVICE_BUFFERSIZE));
}
Part of the XML File...
<?xml version="1.0" encoding="UTF-8"?>
<DeviceDefines>
<Group Name="TM-T Series" Desc="Thermal Point Of Sale Printers">
<Device Name="TM-T88III"
Class="1"
Features="2147610751"
PaperPaths="15"
Fonts="3"
Cols="12"
Interface="15"
Lang="3"
ExFeatures="41"
Buffer="4096">
</Device>
</Group>
</DeviceDefines>
Thanks for your reply!
|
|
|
|
|
I have two CXmlDocumentWrapper objects (doc1 and doc2). And I need to copy xml document from doc1 to doc2. So I must free memory used by doc1.m_xmldoc and copy data from doc2.m_xmldoc to doc1.m_xmldoc. As I can see now you working with pointers, so if I change doc1.m_xmldoc, doc2.m_xmldoc will be changed too. What should I do?
I cannot use Clone method because I must free doc1.m_xmldoc first
|
|
|
|
|
Assume you have doc1 object:
CXmlDocumentWrapper doc2(doc1.Clone());
Creative minds - create creative creations!
|
|
|
|
|
I mean if I have some xml document already loaded in doc2
CXmlDocumentWrapper doc2;
doc2.Load( _T("sample.xml") );
....
doc2 = doc1.Clone();
So I want to know will the memory already occupied in doc2 be free when I do doc2 = doc1.Clone() ?
|
|
|
|
|
Yep.
Creative minds - create creative creations!
|
|
|
|
|
Hi, Alex
I have only one little question: How I can add the string like <?xml version="1.0" encoding="UTF-8"?> in my xml file. I try to do like your suggestion in your old article( insert createProcessingInstruction() in the document constructor). But in this case if I make after then LoadXML(something) to start new tree, the instruction row has been disappeared.
I fixed it now by loading doc from template file, but may be you have a better resolve?
Thanks
|
|
|
|
|
The create processing instructio function is not implemented in this release, but since the class operates on ditect pointers you can ask for raw pointer to the document via Interface() finction and add the instruction yourself.
Creative minds - create creative creations!
|
|
|
|
|
Anyone know what modifications need to be made in order to get this to work with MSXML 4.0 SP2?
Overall, an excellent article alex, thank you!
Dru
|
|
|
|
|
Basically i guess you just need to import msxml4.dll instead of msxml3.dll
And agiain that is only an unchecked guess...
I'll try to check this later when i have time...
Creative minds - create creative creations!
|
|
|
|
|
Yes, but You also have to change the two occurenses of CLSID_DOMDocument to CLSID_DOMDocument40 , or the msxml3.dll will be loaded even though you've #imported "msxml4.dll".
(I struggled with this for a while; I couldn't understand why it would validate my .xml files which contained a reference to a .xsd. Now I know that msxml3 doesn't grok .xsd's.)
Thanks for the classes anyway Alex, even though I hope You will take the time to update it regarding the memory issues. I'm not into this COM stuff, so I can't really do it myself (and I'm lazy, too ).
|
|
|
|
|
Ah that might explain why my application doesn't work at my workplace. I only imported the msxml4.dll and we don't have msxml3.dll installed on our machines. So, the program did nothing.
I'll post the results. I am hopeful now that this was the problem
|
|
|
|
|
My main problem is that i currently dont understand how msxml.3 works. Im not familiar with it, but this article has helped me allot. If I had to I could break down the wrapper to see who msxml works. However, right now im just tring to get away with plug and play.
I need to parse an xml file that has a scheme and database like records at the bottom, it might resemble...
I used the included application to determine how to parse the tags i wanted, but it seems indirect. This is what im doing now...
<br />
void ParseNode(IDispatch *pNode)<br />
{<br />
CXmlNodeWrapper node(pNode);<br />
<br />
if (node.Name() == "row")<br />
{<br />
for (int i = 0; i < node.NumAttributes(); i++)<br />
{<br />
node.GetAttribName(i); <br />
node.GetAttribVal(i);<br />
<br />
}<br />
}<br />
<br />
for (int i = 0; i < node.NumNodes(); i++)<br />
{<br />
ParseNode( node.GetNode(i), szRec, szFileDate );<br />
}<br />
}<br />
And ild like to use the query function, but i cant seem to figure out how to use it. ie...
CXmlNodeWrapper nodeMode( xmlNode.FindNode("//RowsetSchema:row[@Type='Mode']") );
CXmlNodeWrapper nodeKeyr( xmlNode.FindNode("//RowsetSchema:row[@Type='Keyer']") );
CXmlNodeWrapper nodeC3 ( xmlNode.FindNode("//RowsetSchema:row[@Type='C3']") );
CXmlNodeWrapper nodeC4 ( xmlNode.FindNode("//RowsetSchema:row[@Type='C4']") );
So instead of recursivly looping, ild rather just query for each field in each record individually..
Im a little unsure of the syntax fo the FindNode parameter? I greatly appreciate any insight ^^
thx,
-SD
_____________________________________________________________________
〈xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
xmlns:rs='urn:schemas-microsoft-com:rowset'
xmlns:z='#RowsetSchema'〉
〈s:Schema id='RowsetSchema'〉
〈s:ElementType name='row' content='eltOnly' rs:updatable='true'>
〈s:AttributeType name='SortMode' rs:number='1' rs:write='true'>
〈s:datatype dt:type='i1' dt:maxLength='1' rs:precision='0' rs:fixedlength='true' rs:maybenull='false'/〉
〈/s:AttributeType〉
〈s:AttributeType name='Mode' rs:number='2' rs:write='true'>
〈s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='20' rs:precision='0' rs:fixedlength='true' rs:maybenull='false'/〉
〈/s:AttributeType〉
〈s:AttributeType name='Keyer' rs:number='3' rs:write='true'>
〈s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='50' rs:precision='0' rs:fixedlength='true' rs:maybenull='false'/〉
〈/s:AttributeType〉
// ... and so on ....
〈/s:ElementType〉
〈/s:Schema〉
// and here would be the data in the file...
〈rs:data〉
〈rs:insert〉
〈z:row SortMode='11' Mode='COA ADDRESS' Keyer='We**, Ri***' c3='2:34:52' c4='0:00:19'/〉
〈z:row SortMode='10' Mode='COA NON ADDRESS' Keyer='Ti*****, So****' c3='3:37:43' c4='0:00:02'/〉
〈z:row SortMode='10' Mode='COA NON ADDRESS' Keyer='La****, ***cy ' c3='0:57:03' c4='0:00:01'/〉
〈/rs:insert〉
〈/rs:data〉
〈/xml〉
|
|
|
|
|
I got it, Basically , i just needed a way to loop through the records and pull the required fields. Here is what i turned up, course ill have to add it to the api.
<br />
void CXmlDocumentWrapper::Test()<br />
{<br />
long numitems;<br />
<br />
MSXML2::IXMLDOMNodeListPtr items = m_xmldoc->getElementsByTagName("z:row");<br />
<br />
items->get_length(&numitems);<br />
for (int i=0;i < numitems;i++)<br />
{<br />
MSXML2::IXMLDOMNodePtr item ;<br />
items->get_item(i, &item);<br />
_variant_t sz1 = item->attributes->getNamedItem("Mode")->GetnodeValue();<br />
_variant_t sz2 = item->attributes->getNamedItem("Keyer")->GetnodeValue();<br />
}<br />
printf("Found %f Records\n", numitems);<br />
}<br />
|
|
|
|
|
Look at the CXmlNodeListWrapper class, that is what you need,
also user CXmlNodeWrapper::FindNodes , wich returns a node list.
i.e : CXmlNodeListWrapper list(xmlNode.FindNodes("//Z:Row[@Type='sometype']");
Creative minds - create creative creations!
|
|
|
|
|
Hi again!
I might be off here but why does almost every function return a BOOL?
In the msxml3.tli (created by VC.NET from the #import) every call has a:
if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
which throws a _com_error.
So dosent all functions either return TRUE or throw an exception? Or am I missing something elementary (I am not an expert in COM)?
/Aldus
P.s.
Good work with the XML wrapper!
D.s.
|
|
|
|
|
Hi!
I get a link error while compileing the wrapper. Do I need to include some .lib? Does the wrapper work in my MFC project or will it only work with an ATL project? I use VS.NET2003 as compiler.
XmlNodeWrapper.obj : error LNK2019: unresolved external symbol "wchar_t * __stdcall _com_util::ConvertStringToBSTR(char const *)" (?ConvertStringToBSTR@_com_util@@YGPA_WPBD@Z) referenced in function "public: __thiscall _bstr_t::Data_t::Data_t(char const *)" (??0Data_t@_bstr_t@@QAE@PBD@Z)
XmlNodeWrapper.obj : error LNK2019: unresolved external symbol "char * __stdcall _com_util::ConvertBSTRToString(wchar_t *)" (?ConvertBSTRToString@_com_util@@YGPADPA_W@Z) referenced in function "public: char const * __thiscall _bstr_t::Data_t::GetString(void)const " (?GetString@Data_t@_bstr_t@@QBEPBDXZ)
Best regards
Aldus
|
|
|
|
|
Hi again!
I used the wrapper in a regular DLL with MFC statically linked, but now I think I have solved it myself:
I changed the setting in "C/C++ -> Language -> Treat wchar_t as Built-In Type" to "No"
and now it links ok.
Best regards
Aldus
|
|
|
|
|
Yes the wrapper uses MFC so you must specify using MFC in the project properties.
Creative minds - create creative creations!
|
|
|
|
|
I have tested the classes to use them in my application and I have realized that they only work for Doc/View projects (at least in VS2003), not in dialog or console based apps.
In non Doc/View apps it fails in:
<br />
CXmlDocumentWrapper::CXmlDocumentWrapper()<br />
{<br />
m_xmldoc.CreateInstance(MSXML2::CLSID_DOMDocument);<br />
}<br />
It always returns NULL.
How can I modify the classes to make them work in a Windows Service or a console app?
Is it possible or it depends on the dll?
|
|
|
|
|
Make sure to call CoInitialize
Creative minds - create creative creations!
|
|
|
|
|
|
hi alex
i think i may have discovered a memory issue relating to releasing the underlying interfaces that you may want to document.
when initializing an implict _com_ptr_t (as happens everywhere) with an interface pointer, _com_ptr_t constructor AddRef 's as you would expect.
The problem occurs when the wrapper functions call Detach internally on those _com_ptr_t because now the interface has an extra AddRef on it and unless I call Release explicitly on the interface it will not get released until the application ends.
ie.
CXmlDocumentWrapper doc;<br />
<br />
:<br />
<br />
CXmlNodeWrapper node(doc.AsNode());<br />
<br />
:
will leave the document interface hanging around. what's needed is an extra Release like this:
CXmlDocumentWrapper doc;<br />
<br />
:<br />
<br />
IXMLDOMNode* pNode = pDoc->AsNode(); <br />
CXmlNodeWrapper node(pNode);<br />
<br />
:<br />
<br />
pNode->Release();
note: i'm working with large xml files (500k+) where the memory increase is very significant. with the changes i've suggested, the 'leak' goes away.
rgds
.dan.g.
AbstractSpoon Software
|
|
|
|
|
Yes you aer right!!!
Also i beleave that just removing Detach() from the function will do the trick.
The returned _com_ptr will be cast to pointer thus returning the interface and then destroyed thus releasing the interface.
i.e.
Instead of:
MSXML2::IXMLDOMNode* CXmlDocumentWrapper::AsNode()
{
if (!IsValid())
return NULL;
return m_xmldoc->GetdocumentElement().Detach();
}
change to:
MSXML2::IXMLDOMNode* CXmlDocumentWrapper::AsNode()
{
if (!IsValid())
return NULL;
return m_xmldoc->GetdocumentElement();
}
Creative minds - create creative creations!
|
|
|
|
|
thanks. i'll try your suggestion because keeping such details hidden is vastly preferable to my solution of always having to remember.
however, is there not a risk that when the _com_ptr goes out of scope it will release the interface and COM will blitz it before it can be used.
.dan.g.
AbstractSpoon Software
|
|
|
|