wxXML question. Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Dark Alchemist
Super wx Problem Solver
Super wx Problem Solver
Posts: 347
Joined: Wed Nov 02, 2005 10:33 am

wxXML question.

Post by Dark Alchemist »

Code: Select all

        wxXmlNode *node7 = new wxXmlNode(node6,wxXML_ELEMENT_NODE,wxT("ROW"));
        wxString tmp;
        for (list<Recipe>::iterator it = Out.begin(); it!=Out.end(); ++it)
        {
            tmp.Printf("%lu",(*it).xx);
            node7->AddProperty(wxT("XX"),tmp);
        }
The above does not write a well formatted XML structure but this does

Code: Select all

        wxString tmp;
        for (list<Recipe>::iterator it = Out.begin(); it!=Out.end(); ++it)
        {
            wxXmlNode *node7 = new wxXmlNode(node6,wxXML_ELEMENT_NODE,wxT("ROW"));
            tmp.Printf("%lu",(*it).xx);
            node7->AddProperty(wxT("XX"),tmp);
        }
but to my eyes that is just bad coding. Is there any way to do the above without having to make numerous calls to new?
illnatured
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 234
Joined: Mon May 08, 2006 12:31 pm
Location: Krakow, Poland

Post by illnatured »

This is not bad coding. Calling new multiple times is actually what every program do :).
VC++ 2005 / Windows XP / wxWidgets 2.8.9
Dark Alchemist
Super wx Problem Solver
Super wx Problem Solver
Posts: 347
Joined: Wed Nov 02, 2005 10:33 am

Post by Dark Alchemist »

Well, it just looks very untidy to downright sloppy when all I am doing is adding to it. Just seems strange that I am having to create a new object each time when all I am wanting to do is add to the same object.

When I am using new and it allocates the memory does it free all of those new when I leave the function or when I leave the program?
catalin
Moderator
Moderator
Posts: 1618
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: wxXML question.

Post by catalin »

Dark Alchemist wrote:

Code: Select all

        wxXmlNode *node7 = new wxXmlNode(node6,wxXML_ELEMENT_NODE,wxT("ROW"));
        wxString tmp;
        for (list<Recipe>::iterator it = Out.begin(); it!=Out.end(); ++it)
        {
            tmp.Printf("%lu",(*it).xx);
            node7->AddProperty(wxT("XX"),tmp);
        }
The above does not write a well formatted XML structure
Are you adding multiple properties with the same name to the same XML node? Or this is a "simplified" version, in which case maybe you should post something closer to the real thing..
Dark Alchemist
Super wx Problem Solver
Super wx Problem Solver
Posts: 347
Joined: Wed Nov 02, 2005 10:33 am

Re: wxXML question.

Post by Dark Alchemist »

catalin wrote:
Dark Alchemist wrote:

Code: Select all

        wxXmlNode *node7 = new wxXmlNode(node6,wxXML_ELEMENT_NODE,wxT("ROW"));
        wxString tmp;
        for (list<Recipe>::iterator it = Out.begin(); it!=Out.end(); ++it)
        {
            tmp.Printf("%lu",(*it).xx);
            node7->AddProperty(wxT("XX"),tmp);
        }
The above does not write a well formatted XML structure
Are you adding multiple properties with the same name to the same XML node? Or this is a "simplified" version, in which case maybe you should post something closer to the real thing..
That is the real thing. It simply adds the nodes one after another until it is done. It works 100% (I am leaving out the header information that is static) but, in essence, what I am doing is trying to get this static command to work in a dynamic environment.

When the above executes it even finishes the lines with " /> " but if I do it the first way that I showed it does not add the " /> " which messes up the xml.

What I am looking for is a way to append the same data where the information in the data changes and could be 1 to 1 million lines long. Basically an append.
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

What I am looking for is a way to append the same data where the information in the data changes and could be 1 to 1 million lines long. Basically an append.
So, if i understand correctly, you do :

Code: Select all

<foo
data="dssfadsfdsfdsfd"
data="fdsfdsfdsfdsfds"
data="dffdsfdsfdsfdsf"
/>
?
If so, I'm pretty sure it's illegal XML, so expecting XML toolkits to support that may be a bad assumption.

I'd rather recommend :

Code: Select all

<foo>
dssfadsfdsfdsfd
fdsfdsfdsfdsfds
dffdsfdsfdsfdsf
<foo/>
"Keyboard not detected. Press F1 to continue"
-- Windows
Dark Alchemist
Super wx Problem Solver
Super wx Problem Solver
Posts: 347
Joined: Wed Nov 02, 2005 10:33 am

Post by Dark Alchemist »

Auria wrote:
What I am looking for is a way to append the same data where the information in the data changes and could be 1 to 1 million lines long. Basically an append.
So, if i understand correctly, you do :

Code: Select all

<foo
data="dssfadsfdsfdsfd"
data="fdsfdsfdsfdsfds"
data="dffdsfdsfdsfdsf"
/>
?
If so, I'm pretty sure it's illegal XML, so expecting XML toolkits to support that may be a bad assumption.

I'd rather recommend :

Code: Select all

<foo>
dssfadsfdsfdsfd
fdsfdsfdsfdsfds
dffdsfdsfdsfdsf
<foo/>
My reader has no problems with this XML.

Code: Select all

<?xml version="1.0" encoding="utf-8" ?> 
- <DATAPACKET Version="2.0">
- <METADATA>
- <FIELDS>
  <FIELD attrname="BAD" fieldtype="string" WIDTH="50" /> 
  <FIELD attrname="Name" fieldtype="string" WIDTH="60" /> 
  <FIELD attrname="LOOK" fieldtype="string" WIDTH="5" /> 
  <FIELD attrname="LISTEN" fieldtype="string" WIDTH="37" /> 
  <FIELD attrname="GetZealous" fieldtype="string" WIDTH="8" /> 
  <FIELD attrname="Book" fieldtype="string" WIDTH="60" /> 
  <FIELD attrname="Of" fieldtype="string" WIDTH="60" /> 
  <FIELD attrname="Mighty" fieldtype="string" WIDTH="5" /> 
  <FIELD attrname="Love" fieldtype="string" WIDTH="1" /> 
  </FIELDS>
  <PARAMS /> 
  </METADATA>
- <ROWDATA>
  <ROW BAD="123456" Name="Susan" LOOK="Nice" LISTEN="Does" GetZealous="Sometimes" Book="Oh, yes" Of="Maybe" Mighty="Very" Love="Possibly" /> 
  <ROW BAD="7642456" Name="Karen" LOOK="Nice" LISTEN="Does" GetZealous="Sometimes" Book="Sometimes" Of="Maybe" Mighty="Very" Love="Possibly" /> 
  <ROW BAD="89067" Name="Listerine" LOOK="Alright" LISTEN="Hardly" GetZealous="Naw" Book="Possibly" Of="Maybe" Mighty="Iffy" Love="Not Right Now" /> 
  </ROWDATA>
  </DATAPACKET>
Notice those rows? They change, hence are dynamic, but the command I use isn't. So, again, is the second way the only way to achieve what it is I am after?
tbreina
Moderator
Moderator
Posts: 1289
Joined: Thu Nov 25, 2004 7:38 pm
Location: Coronado, CA

Post by tbreina »

Dark Alchemist wrote:Well, it just looks very untidy to downright sloppy when all I am doing is adding to it. Just seems strange that I am having to create a new object each time when all I am wanting to do is add to the same object.

When I am using new and it allocates the memory does it free all of those new when I leave the function or when I leave the program?
I was under the impression that you need to explicitly "delete" something that is created with "new". Otherwise, you'll have a memory leak on the heap. For example,

http://www.cplusplus.com/doc/tutorial/dynamic/

The only place where I think this doesn't apply is for the wxWidgets components that are added to a wxFrame/wxDialog. IIRC, they are automatically deleted when their parent window is destroyed. However, I think your wxXML should be explicitly deleted.

Of course, there are memory leak testers out there that can tell you if your "new"s are lingering in RAM after your program terminates. That's at least one way to test what is really going on.

-Tony
Everybody's got something to hide except for me and my monkey.
tbreina
Moderator
Moderator
Posts: 1289
Joined: Thu Nov 25, 2004 7:38 pm
Location: Coronado, CA

Re: wxXML question.

Post by tbreina »

Dark Alchemist wrote:

Code: Select all

        wxXmlNode *node7 = new wxXmlNode(node6,wxXML_ELEMENT_NODE,wxT("ROW"));
        wxString tmp;
        for (list<Recipe>::iterator it = Out.begin(); it!=Out.end(); ++it)
        {
            tmp.Printf("%lu",(*it).xx);
            node7->AddProperty(wxT("XX"),tmp);
        }
The above does not write a well formatted XML structure but this does

Code: Select all

        wxString tmp;
        for (list<Recipe>::iterator it = Out.begin(); it!=Out.end(); ++it)
        {
            wxXmlNode *node7 = new wxXmlNode(node6,wxXML_ELEMENT_NODE,wxT("ROW"));
            tmp.Printf("%lu",(*it).xx);
            node7->AddProperty(wxT("XX"),tmp);
        }
but to my eyes that is just bad coding. Is there any way to do the above without having to make numerous calls to new?
What output are you seeing? What does "well formatted" mean specifically?
Everybody's got something to hide except for me and my monkey.
Dark Alchemist
Super wx Problem Solver
Super wx Problem Solver
Posts: 347
Joined: Wed Nov 02, 2005 10:33 am

Post by Dark Alchemist »

tbreina wrote:
Dark Alchemist wrote:Well, it just looks very untidy to downright sloppy when all I am doing is adding to it. Just seems strange that I am having to create a new object each time when all I am wanting to do is add to the same object.

When I am using new and it allocates the memory does it free all of those new when I leave the function or when I leave the program?
I was under the impression that you need to explicitly "delete" something that is created with "new". Otherwise, you'll have a memory leak on the heap. For example,

http://www.cplusplus.com/doc/tutorial/dynamic/

The only place where I think this doesn't apply is for the wxWidgets components that are added to a wxFrame/wxDialog. IIRC, they are automatically deleted when their parent window is destroyed. However, I think your wxXML should be explicitly deleted.

Of course, there are memory leak testers out there that can tell you if your "new"s are lingering in RAM after your program terminates. That's at least one way to test what is really going on.

-Tony
Exactly my thought on the subject. Where would I put the delete since I am appending? If I delete it before it is written I have no idea what would happen but I don't think it would good.

What I have always wondered about wxXML is where is the append or append like command? In a 100% static enviornment the commands are simple and no real need but when you have no idea how many rows is coming at you then how do you handle it so it would write the xml as I posted? I posted 3 rows but it could be 1 million "ROW" as well.
Dark Alchemist
Super wx Problem Solver
Super wx Problem Solver
Posts: 347
Joined: Wed Nov 02, 2005 10:33 am

Re: wxXML question.

Post by Dark Alchemist »

tbreina wrote:
Dark Alchemist wrote:

Code: Select all

        wxXmlNode *node7 = new wxXmlNode(node6,wxXML_ELEMENT_NODE,wxT("ROW"));
        wxString tmp;
        for (list<Recipe>::iterator it = Out.begin(); it!=Out.end(); ++it)
        {
            tmp.Printf("%lu",(*it).xx);
            node7->AddProperty(wxT("XX"),tmp);
        }
The above does not write a well formatted XML structure but this does

Code: Select all

        wxString tmp;
        for (list<Recipe>::iterator it = Out.begin(); it!=Out.end(); ++it)
        {
            wxXmlNode *node7 = new wxXmlNode(node6,wxXML_ELEMENT_NODE,wxT("ROW"));
            tmp.Printf("%lu",(*it).xx);
            node7->AddProperty(wxT("XX"),tmp);
        }
but to my eyes that is just bad coding. Is there any way to do the above without having to make numerous calls to new?
What output are you seeing? What does "well formatted" mean specifically?
HUH? The code I posted about with the rowdata and ROW is what it looks like in ie but, since you know delphi, that code loads up in a Delphi dataset 100% perfect.
tbreina
Moderator
Moderator
Posts: 1289
Joined: Thu Nov 25, 2004 7:38 pm
Location: Coronado, CA

Post by tbreina »

Perhaps you need to do this in two steps.

1. Create a wxXMLProperty (http://docs.wxwidgets.org/stable/wx_wxx ... mlproperty). You can use the SetNext function to keep adding to your row within your loop.

2. When you've finished the loop, you'll add your wxXMLProperty to your wxXMLNode via AddProperty(wxXmlProperty* prop)

-Tony

Code: Select all

wxXMLProperty nodeP;
wxString tmp;

for (list<Recipe>::iterator it = Out.begin(); it!=Out.end(); ++it)
{
   tmp.Printf("%lu",(*it).xx);
   nodeP.SetNext(wxXMLProperty(wxT("XX"),tmp, NULL));
}

wxXMLNode node7;
node7.AddProperty(nodeP);

Everybody's got something to hide except for me and my monkey.
Dark Alchemist
Super wx Problem Solver
Super wx Problem Solver
Posts: 347
Joined: Wed Nov 02, 2005 10:33 am

Post by Dark Alchemist »

That looks so much tidier but I don't see how it will handle each "ROW". I already addproperty to the node7 but how would I do a new row which doesn't do another new node7.

Each line has to be

Code: Select all

<ROW BAD="123456" Name="Susan" LOOK="Nice" LISTEN="Does" GetZealous="Sometimes" Book="Oh, yes" Of="Maybe" Mighty="Very" Love="Possibly" /> 
for it to work and we know that each of those "=" is an AddProperty so those are taken care of already.
tbreina
Moderator
Moderator
Posts: 1289
Joined: Thu Nov 25, 2004 7:38 pm
Location: Coronado, CA

Post by tbreina »

Dark Alchemist wrote:That looks so much tidier but I don't see how it will handle each "ROW". I already addproperty to the node7 but how would I do a new row which doesn't do another new node7.

Each line has to be

Code: Select all

<ROW BAD="123456" Name="Susan" LOOK="Nice" LISTEN="Does" GetZealous="Sometimes" Book="Oh, yes" Of="Maybe" Mighty="Very" Love="Possibly" /> 
for it to work and we know that each of those "=" is an AddProperty so those are taken care of already.
Wouldn't that just be

node7.SetNext( nodeP )

?
Everybody's got something to hide except for me and my monkey.
Dark Alchemist
Super wx Problem Solver
Super wx Problem Solver
Posts: 347
Joined: Wed Nov 02, 2005 10:33 am

Post by Dark Alchemist »

No idea but I will test it though I have just been multi-tasking too much and need a break. Your code looks like it should work and doesn't needlessly do all of the new so that is wonderful right there. I suspect it will work but I have never used the SetNext.

edit: Doesn't compile and I did include <wx/xml/xml.h>

errors

Code: Select all

`wxXMLProperty' was not declared in this scope
`wxXMLNode' was not declared in this scope
Post Reply