|
Hi,
I m just trying to use your wrapper to play with XML,
i just created a dialog application and tried to use Load function.
First of all i created CXmlDocumentWrapper m_xmlDoc; in the header file and then onOK function
i m calling m_xmlDoc.Load("C:\\sample.xml"); but when i debug i found out its returing at this line if (!IsValid())
return FALSE;
kidnly let me know, what do i have to do.
Regards,
-- modified at 13:24 Monday 17th October, 2005
|
|
|
|
|
Either your XML file is not well formed. Or there is some other error.
You should provide more code and the sample xml doc , for me to be able to help you.
Creative minds - create creative creations!
|
|
|
|
|
Thanks, just came cross to this article again....so noticed i forgot the reply it.
i was doing a silly mistake that why i was having that probolem...just explainig it here so that if any1else getting the same problem then it would be easy to get solution without spending hrs to located the problem.
Solution was straight forward since MSXML is COM base and we have to always Coinitialize it to NULL. So i had to just call CoInitialize(NULL) from InitInstance().
Regards,
|
|
|
|
|
Hi when I load the document I always get FALSE
My code:
m_xmlDoc.Load("Downloads.xml");
if (m_xmlDoc.IsValid()==TRUE) MessageBox("TRUE");
else MessageBox("FALSE");
Decleration of m_xmlDoc:
CXmlDocumentWrapper m_xmldoc;
When I opend the file with your program I can list the XML, but when I want to open the file with my program it dosnt work I also called CoIntialize(NULL) in InitInstance() and I always get FALSE message, I also included all of the headers that you included in your program.
PLZ help || advice && MessageBox("Have a nice Day ");
|
|
|
|
|
I changed the default ctor as follows, and it seems to pass the IsValid() function. Although I'm still having a problem loading a XML file..
CXmlDocumentWrapper::CXmlDocumentWrapper()
{
CoInitialize(NULL);
try
{
m_xmldoc = NULL;
m_xmldoc.CreateInstance(MSXML2::CLSID_DOMDocument);
}
catch(_com_error &e)
{
CString strError(_T(e.ErrorMessage()));
CString strDescription(_T((LPCSTR) e.Description()));
}
}
|
|
|
|
|
Hola a todos,
En especial a Alex Hazanov,
He probado tus XMLNodeWrapper.h y .cpp en mi aplicacion y no funciona, he llevado todo a la tuya y si me va, pero en la mia no, me falla en el
m_xmlDoc.Load()
no me lee la ruta que le pongo, la he puesto de mil formas y he visto que
BOOL CXmlDocumentWrapper::IsValid()
{
if (m_xmldoc == NULL)
{
AfxMessageBox("Hola");
return FALSE;
}
if (m_xmldoc.GetInterfacePtr() == NULL)
{
AfxMessageBox("Adios");
return FALSE;
}
return TRUE;
}
se me va en el primero, no se pq pero el m_xmldoc me dice q es null
pq es???
Que alguien me de una solucion, llevo mas de 1 mes con ese error y no me lo puedo permitir
Escribirme a ruespla@upvnet.upv.es
Gracias
|
|
|
|
|
Sorry my spanish is not as good as my programming skills
I can only guess that you are trying to use the document wrapper and you either fail to load a document or you are trying to use an uninitialized document.
So you must note that :
1. To load a document you have to call the Load() function passing a full path name to the xml file ==> xmlDoc.Load("C:\\Some Folder\\some_xml_doc.xml");
2. To use the document without an xml file (build it from scratch) you have to load in as an xml string at least the root node, without it the document will not have the root node ==> xmlDoc.LoadXML("<SomeRoot></SomeRoot>");
Creative minds - create creative creations!
|
|
|
|
|
Ruben,I guess you problem is that you forgot to to call CoInitialize(NULL) from InitInstance() ..or some other suitable place..
Now in spanish as well:Creo que tu problema es que te olvidaste de llamar a CoInitialize(NULL) desde InitInstance() ...o en otro lugar correspondiente
HTH (Hope that helps)
-- Ricky Marek (AKA: rbid)
-- "Things are only impossible until they are not" --- Jean-Luc Picard
My articles
|
|
|
|
|
Hello!
Here are a few more functions, perhaps useful to others. First off, I would suggest making all accessors const, and I also changed CString to std::string. For me, this was more convenient. Anyway, here are the functions:
<code>
string CXmlNodeWrapper::GetNamespaceURI() const
{
if( IsValid() )
{
_bstr_t str = static_cast<_bstr_t>(m_xmlnode->GetnamespaceURI());
return static_cast<char*>(str);
}
return "";
}
string CXmlNodeWrapper::GetNodeValue() const
{
if( IsValid() ) {
_bstr_t str = static_cast<_bstr_t>(m_xmlnode->GetnodeValue());
return static_cast<char*>(str);
}
return "";
}
MSXML2::IXMLDOMNodePtr CXmlNodeWrapper::InsertNode(int index, LPCTSTR nodeName, LPCTSTR nameSpace)
{
MSXML2::IXMLDOMDocumentPtr xmlDocument = m_xmlnode->GetownerDocument();
if (xmlDocument)
{
MSXML2::IXMLDOMNodePtr newNode = xmlDocument->createNode(_variant_t((short)MSXML2::NODE_ELEMENT), nodeName, nameSpace);
MSXML2::IXMLDOMNodePtr refNode = GetNode(index);
if (refNode)
newNode = m_xmlnode->insertBefore(newNode,_variant_t(refNode.GetInterfacePtr()));
else
newNode = m_xmlnode->appendChild(newNode);
return newNode;
}
return NULL;
}
void CXmlDocumentWrapper::CreateRootElement(LPCSTR name, LPCTSTR nameSpace)
{
MSXML2::IXMLDOMElementPtr pe;
pe = m_xmldoc->createNode("element", name, nameSpace);
if( pe!=NULL )
{
m_xmldoc->appendChild(pe);
pe.Release();
}
}
string CXmlDocumentWrapper::GetParseError() const
{
if( IsValid() )
{
MSXML2::IXMLDOMParseErrorPtr error = m_xmldoc->GetparseError();
boost::format f("Error:\nCode = 0x%1$x\nSource = Line: %2%, Char: %3%\nError Description = %4%");
_bstr_t bstrErr(error->Getreason());
f % error->GeterrorCode() % error->Getline() % error->Getlinepos() % (char*)bstrErr;
return f.str();
}
else
return "";
}
void CXmlDocumentWrapper::CreateProcessingInstruction(LPCTSTR target, LPCTSTR data)
{
MSXML2::IXMLDOMProcessingInstructionPtr pi;
pi = m_xmldoc->createProcessingInstruction(target, data);
if( pi!=NULL )
{
m_xmldoc->appendChild(pi);
pi.Release();
}
}
</code>
That's it.
Thanks for a great set of wrappers!
Daniel Lidström
|
|
|
|
|
Thanks Daniel.
I'm very happy to see that someone is not only blindly using the the wrapper (although that is perfectly OK , that was the intention), but also developing and farther enriching it.
Again , thank you very much.
Creative minds - create creative creations!
|
|
|
|
|
Hi Alex,
Why won't you update your excellent class with those ammenments then?
I'm observing it now to see how it will fit to my current application. If it will, you will absolutely get a 5!
Stilgar.
|
|
|
|
|
Sorry but I don't know how to create an xml like
<?xml verion = "1.0" encoding = "UTF-8"?>
<root>
<attb name = "1">
<ele name = "1.1">
</attb>
<attb name = "2">
<ele name = "2.1">
</attb>
</root>
I called:
doc.CreateProcessingInstruction("xml", "1.0");
doc.LoadXML("<root></root>");
...
but it didn't at <?xml = "1.0"?> at the beginning of the doc
I called:
doc.LoadXML("<root></root>");
doc.CreateProcessingInstruction("xml", "1.0");
...
it insert <?xml = "1.0"?> after <root> element.
Please help! Thank you in advanced!
|
|
|
|
|
Hello!
How do I access the node value of a text node. For example, the xml is this:
<?xml version="1.0" encoding="utf-8"?>
<Model mod_att1="val1" mod_att2="val2">__model_value__
<MainLine att1="val1" att2="val2">__mainline_value__
</MainLine>
</Model>
If I have a CXmlNodeWrapper pointing to the top element, Model, and I call GetText, then I receive a string with a value of
"__model_value__
__mainline_value__"
How do I get the value of the Model element? I'm only interested in the __model_value__ part.
Thanks in advance!
|
|
|
|
|
I figured it out. Add a method to CXmlNodeWrapper like this:
class CXmlNodeWrapper
{
...
//! Access the value of the node
std::string GetNodeValue() const;
};
string CXmlNodeWrapper::GetNodeValue() const
{
if( IsValid() ) {
_bstr_t str = static_cast<_bstr_t>(m_xmlnode->GetnodeValue());
return static_cast<char*>(str);
}
return "";
}
This method may only be called when NodeType() returns "text".
|
|
|
|
|
Hello :->
As I can't find a solution for my GetUrl() problem, I changed my objective :
I would like to use the "Serialize" function to save my DOM....like that, I don't care of all open/save functions
Is it possible to serialize my DOM ?
Bye.
|
|
|
|
|
I am going to use MSXML 3.0 in my project. I have a time critical process where i need to generate a big XML file within 3 sec This is the maximum amount of time avaiable for the process to complete.Can MSXML 3.0 do it for me. Anybody any comments.
|
|
|
|
|
Of course it can, it has only about 10000000 different "ifs".
Such as what prosessor do you have, what is the speed of your hard disk, wether your process has the highest priority , what processor time is available for your process, how big is your file going to be, is the mashine doing something elese at this moment (updating the untivirus for example) , etc...
What I'm trying to say is that you should NEVER under no circumstanses expect some task finish in some predefined time , please either work on some other strategy in your application or at least explain why is the time so critical.
Creative minds - create creative creations!
|
|
|
|
|
Why the process is time critical ? the answer to that is , i am generating an export for a device configuration , the configuration is saved as an xml file . Now when the user exports the configuration , i dont have any status bar for indicating the status of the export, thus the application which i am using for export appears to be hung. I dont want to spoil the user experience by taking too long a time to generate an export,thats why a self imposed time criticality is there.I was talking to one of my friends about MSXML 3.0 taking too long to taverse the DOM tree if the XML document is too big , I wanted your opinion on weather MSXML is slow . I have worked with it earlier and i found it really fast , generating an xml in milli seconds.But i still need a confiration from someone before i go ahead.After reading the article i felt you might be the one. Thanx in advance for paying attention.
|
|
|
|
|
MSXML is capble of producing 50000 nodes XML in less than a second.
It also capble of quiering this XML 5~50 quieries in less than a second.
But as I said before it all depends on many issues.
My advise for you is to try first, than if you see that the process of saving the configuration is taking too long (more than couple of seconds) consined adding a progress bar or if that is not possible, at least some flashing message like "Saving configuration, please wait...".
Creative minds - create creative creations!
|
|
|
|
|
Hi,
first : thanks for your good work.
I'm trying to use your function GetUrl() to know if the document has been already saved, but it doesn't work.
<br />
if(_myDoc.GetUrl() != ""){<br />
return (_myDoc.Save(""));<br />
}else{<br />
CFileDialog fDlg(FALSE,"xml",NULL,OFN_HIDEREADONLY | OFN_PATHMUSTEXIST,"Xml Files (*.xml)|*.xml||");<br />
...<br />
Can you give me a clue ?
Thx !
|
|
|
|
|
What is it exactly that doesn't work?
What do you get from the GetUrl function?
Creative minds - create creative creations!
|
|
|
|
|
I always get an empty string...and that's strange because always use Load or Save before.
Debug-mode says that returned PTR for url is invalid and converts it to "" (???)
Thx for help
|
|
|
|
|
Hi~ Alex:
First I would like to thank you for your wrapper class. It is a perfect class and help me a lot.
However, I have one little question that need you help to answer.
1. I had used your class and I can add a new node in xml file. But when I open the xml by text editor like UltraEdit. It did not have the line feed effection.
For example:
////////////////////////////////////////////////
// original
<book id="bk112">
<author>Galos, Mike
<title>Visual Studio 7: A Comprehensive Guide
<genre>Computer
<price>49.95
<publish_date>2001-04-16
<description>Microsoft Visual Studio 7 is explored in depth, looking at how Visual Basic, Visual C++, C#, and ASP+ are integrated into a comprehensive development environment.
////////////////////////////////////////////////
// New item
<book_user id="123"><arthur>Keven Yang<title>This is a test<grene>test<date>20050728<description>This is a test item
If you have free time, please help me to answer it!
thanks...
|
|
|
|
|
You should alter the "preserveWhiteSpace" property of DOMDocument.
This requires you to change the originginal code!
Creative minds - create creative creations!
|
|
|
|
|
Hi, I’m new to using XML but would like to incorporate it into one of the applications I’m writing. I am very clueless about both XML in general and using this class. I’ve painfully tried many things and just can’t seem to get the output I’m looking for.
First of all, here is an XML example of what I’m trying to do:
<PrinterDefines>
<Group>Epson SIDM Printers
<Printer>LQ-570
<Lang>ESC/P2</Lang>
<Pins>24</Pins>
</Printer>
<Printer>FX-870
<Lang>ESC/P2</Lang>
<Pins>9</Pins>
</Printer>
</Group>
<Group>Epson POS Printers
<Printer>TM-U220
<Lang>ESC/POS</Lang>
<Pins>9</Pins>
</Printer>
</Group>
</PrinterDefines>
Ideally, I’m defining printer groups and types. The groups in this example are Epson SIDM printers and Epson POS printers. Under each group, there are printers listed. For example, under the SIDM group, there are two printers defined: LQ-570 and FX-870. Under each printer there are further fields pertaining to that printer. For example, the LQ-570 uses ESC/P2 language and it has 24 pins.
What I’m trying to do is obviously retrieve this data. For example, if I want to retrieve the LQ-570, I also want to retrieve it’s language and the number of pins associated with it; and I want these individual strings to end-up in CStrings. Of course, these are two of many fields that I need to associate with that printer. This one is just a start so that I can understand it. Once I understand it, the list will be quite complex, and this data will end up in various classes. For now, CStrings will be fine for me to learn.
Anyway, I can successfully get the entire list of ‘Groups’ with the following:
CXmlNodeListWrapper GroupList(node.FindNodes("//Group"));
My problem is I don’t know what to do next! I’ve tried many things, and each is way way off, so any help would greatly be appreciated.
Thanks!
|
|
|
|