473,395 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Insert XML string into XML File: Part 2

Thanks to some good help from a previous post, I have been able to create
well formed xml as part of a report logger app. However, I still have a
small problem. When I add new xml to the log file, the new nodes are
appended to the original file. What I really want is for the log file to
grow as "report nodes" are added. Also, I am a bit concerned about
performance, particularly as the file grows in size. I must write to an xml
file (vs. a database) and am trying to understand the best way to do this. I
prefer to not use "temp files" and would like to just modify the log file
structure "in-place". Any advice is appreciated. Also, can anyone recommend
a good book that covers XML issues when programming with Managed VC++? The
reports I am generating will eventually be analyzed using Excel or some
other tools such as Crystal Reports.

The follow is a test app that illustrates the problem.

// This is the main project file for VC++ application project
// generated using an Application Wizard.
#include "stdafx.h"
#using <mscorlib.dll>
#using <System.dll>
#using <System.Xml.dll>
//
using namespace std;
using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::Xml;
int _tmain()
{
FileStream* fs;
XmlDocument* xmlReport;
// Test XML Snippet - emulates a test outcome
String* xmlStr = S"<TestResult><Name>Application Load
Test</Name><StartTime>21/05/2006
14:45:58:155</StartTime><StopTime>21/05/2006
14:46:35:530</StopTime><TestResult>FAIL</TestResult><TestAtoms><TestAtom><Name>RateTest
1</Name><StartTime>21/05/2006 14:46:20:530</StartTime><StopTime>21/05/2006
14:46:35:530</StopTime><TestResult>FAIL</TestResult><Duration>15</Duration><Direction>0</Direction><MinRate>255</MinRate><MaxRate>255</MaxRate></TestAtom></TestAtoms></TestResult>";
// the log file
String* sTestLogFile = S"D:\\Development\\LogTest\\ResultLog.xml";
//
try {
if(File::Exists(sTestLogFile) == true) {
// file already exist
fs = File::Open(sTestLogFile, FileMode::Open, FileAccess::ReadWrite,
FileShare::None);
xmlReport = new XmlDocument;
// Open from Stream
xmlReport->Load(fs);
XmlDocumentFragment* docFrag = xmlReport->CreateDocumentFragment();
docFrag->InnerXml = xmlStr;
XmlNode* ResultsNode =
xmlReport->DocumentElement->SelectSingleNode("TestResults");
// ResultsNode->AppendChild(docFrag);
ResultsNode->PrependChild(docFrag);
//xmlReport->DocumentElement->PrependChild(parentNode);
} else {
// no log file currently exist so we create a new file
fs = File::Open(sTestLogFile, FileMode::CreateNew, FileAccess::ReadWrite,
FileShare::None);
xmlReport = new XmlDocument();
XmlDeclaration* xmlDeclaration = xmlReport->CreateXmlDeclaration(S"1.0",
S"utf-8", NULL);
// Create the root element
XmlElement* rootNode = xmlReport->CreateElement("TestLog");
xmlReport->InsertBefore(xmlDeclaration, xmlReport->DocumentElement);
xmlReport->AppendChild(rootNode);
// Create a new <TestResults> element and add it to the root node
XmlElement* parentNode = xmlReport->CreateElement("TestResults");
xmlReport->DocumentElement->PrependChild(parentNode);
parentNode->InnerXml = xmlStr;
}
xmlReport->Save(fs);
fs->Close();
}
catch(XmlException* e)
{
String* msg = e->get_Message();
}
catch(Exception* e)
{
String* msg = e->get_Message();
}
return 0;
}

Output when no file exist GOOD:!

<?xml version="1.0" encoding="utf-8"?>
<TestLog>
<TestResults>
<TestResult>
<Name>Application Load Test</Name>
<StartTime>21/05/2006 14:45:58:155</StartTime>
<StopTime>21/05/2006 14:46:35:530</StopTime>
<TestResult>FAIL</TestResult>
<TestAtoms>
<TestAtom>
<Name>RateTest 1</Name>
<StartTime>21/05/2006 14:46:20:530</StartTime>
<StopTime>21/05/2006 14:46:35:530</StopTime>
<TestResult>FAIL</TestResult>
<Duration>15</Duration>
<Direction>0</Direction>
<MinRate>255</MinRate>
<MaxRate>255</MaxRate>
</TestAtom>
</TestAtoms>
</TestResult>
</TestResults>
</TestLog>

Output when file exist (see above) BAD!

<?xml version="1.0" encoding="utf-8"?>
<TestLog>
<TestResults>
<TestResult>
<Name>Application Load Test</Name>
<StartTime>21/05/2006 14:45:58:155</StartTime>
<StopTime>21/05/2006 14:46:35:530</StopTime>
<TestResult>FAIL</TestResult>
<TestAtoms>
<TestAtom>
<Name>RateTest 1</Name>
<StartTime>21/05/2006 14:46:20:530</StartTime>
<StopTime>21/05/2006 14:46:35:530</StopTime>
<TestResult>FAIL</TestResult>
<Duration>15</Duration>
<Direction>0</Direction>
<MinRate>255</MinRate>
<MaxRate>255</MaxRate>
</TestAtom>
</TestAtoms>
</TestResult>
</TestResults>
</TestLog><?xml version="1.0" encoding="utf-8"?>
<TestLog>
<TestResults>
<TestResult>
<Name>Application Load Test</Name>
<StartTime>21/05/2006 14:45:58:155</StartTime>
<StopTime>21/05/2006 14:46:35:530</StopTime>
<TestResult>FAIL</TestResult>
<TestAtoms>
<TestAtom>
<Name>RateTest 1</Name>
<StartTime>21/05/2006 14:46:20:530</StartTime>
<StopTime>21/05/2006 14:46:35:530</StopTime>
<TestResult>FAIL</TestResult>
<Duration>15</Duration>
<Direction>0</Direction>
<MinRate>255</MinRate>
<MaxRate>255</MaxRate>
</TestAtom>
</TestAtoms>
</TestResult>
<TestResult>
<Name>Application Load Test</Name>
<StartTime>21/05/2006 14:45:58:155</StartTime>
<StopTime>21/05/2006 14:46:35:530</StopTime>
<TestResult>FAIL</TestResult>
<TestAtoms>
<TestAtom>
<Name>RateTest 1</Name>
<StartTime>21/05/2006 14:46:20:530</StartTime>
<StopTime>21/05/2006 14:46:35:530</StopTime>
<TestResult>FAIL</TestResult>
<Duration>15</Duration>
<Direction>0</Direction>
<MinRate>255</MinRate>
<MaxRate>255</MaxRate>
</TestAtom>
</TestAtoms>
</TestResult>
</TestResults>
</TestLog>

May 22 '06 #1
4 2038


SteveW wrote:

FileStream* fs;
XmlDocument* xmlReport;
// Test XML Snippet - emulates a test outcome
String* sTestLogFile = S"D:\\Development\\LogTest\\ResultLog.xml";
//
try {
if(File::Exists(sTestLogFile) == true) {
// file already exist
fs = File::Open(sTestLogFile, FileMode::Open, FileAccess::ReadWrite,
FileShare::None);
xmlReport = new XmlDocument;
// Open from Stream
xmlReport->Load(fs);
I think the problem is not with the DOM code (e.g.
XmlDocument.CreateXXX, AppendChild, PrependChild) you use but simply
with that file stream you use and reuse. Above if the Load call is done
the stream is read and is subsequently positioned at its very end and
then when you do
xmlReport->Save(fs);


the Save call writes to the end of the stream and you will get the
markup duplicated.
So you need to ensure that your Save call overwrites an existing file
and does not append to it.

One way to do that would be (pseudo code)

String* sTestLogFile = S"D:\\Development\\LogTest\\ResultLog.xml";
if(File::Exists(sTestLogFile) == true) {

xmlReport = new XmlDocument();
xmlReport.Load(sTestLogFile)
}

xmlReport.Save(sTestLogFile);

so not using any FileStream at all but using the higher level overloads
of the Load and Save methods which simply take a string with a file name.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
May 22 '06 #2
Also, it seems that you haven't gotten rid of the problem of the "<?xml
version="1.0" encoding="utf-8"?> " being inserted every time you append
an XML fragment to your file.

May 22 '06 #3


Cerebrus wrote:
it seems that you haven't gotten rid of the problem of the "<?xml
version="1.0" encoding="utf-8"?> " being inserted every time you append
an XML fragment to your file.


If he uses the suggestion I made then there will be no duplicated XML
declaration. Currently his codes safes the complete XML document
(including the XML declaration and all other nodes) to the end of the
stream instead of overwriting the same file.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
May 22 '06 #4
Martin -
Your advice really helped. I use a file stream when the file does not exist
to create a new file; but when the file does exist, I use the log file path
to load the XMLDocument as you suggested. This works well and I consider the
problem resolved. Thanks MVP's for all the good help! I am really interested
in digging into this more - can anyone recommend a good book that details
all the various XML methods? I primarily use managed vc++, so something with
a emphases on vc++ is best.

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:eG****************@TK2MSFTNGP04.phx.gbl...


SteveW wrote:

FileStream* fs;
XmlDocument* xmlReport;
// Test XML Snippet - emulates a test outcome


String* sTestLogFile = S"D:\\Development\\LogTest\\ResultLog.xml";
//
try {
if(File::Exists(sTestLogFile) == true) {
// file already exist
fs = File::Open(sTestLogFile, FileMode::Open, FileAccess::ReadWrite,
FileShare::None);
xmlReport = new XmlDocument;
// Open from Stream
xmlReport->Load(fs);


I think the problem is not with the DOM code (e.g. XmlDocument.CreateXXX,
AppendChild, PrependChild) you use but simply with that file stream you
use and reuse. Above if the Load call is done the stream is read and is
subsequently positioned at its very end and then when you do
xmlReport->Save(fs);


the Save call writes to the end of the stream and you will get the markup
duplicated.
So you need to ensure that your Save call overwrites an existing file and
does not append to it.

One way to do that would be (pseudo code)

String* sTestLogFile = S"D:\\Development\\LogTest\\ResultLog.xml";
if(File::Exists(sTestLogFile) == true) {

xmlReport = new XmlDocument();
xmlReport.Load(sTestLogFile)
}

xmlReport.Save(sTestLogFile);

so not using any FileStream at all but using the higher level overloads of
the Load and Save methods which simply take a string with a file name.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

May 23 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: 3c273 | last post by:
Hello, I have a really simple Access database table with a format similar to this: CustomerName - ProductOrdered - QtyOrdered I have a CSV file with the appropriate values as follows:...
3
by: jason | last post by:
How does one loop through the contents of a form complicated by dynamic construction of checkboxes which are assigned a 'model' and 'listingID' to the NAME field on the fly in this syntax:...
5
by: me | last post by:
I'm also having problems getting the bulk insert to work. I don't know anything about it except what I've gleened from BOL but I'm not seeming to get anywhere...Hopefully there is some little (or...
2
by: Bill | last post by:
I'm having what seems to me to be an odd problem. Perhaps there is some explanation, but don't know at this point. Basically I have a form that tracks memberships and donations. The main form...
7
by: tano | last post by:
Hello, I have to insert a char in the middle of a string, I have written two functions but I don't know what is the better? The problem is: if I use malloc() I copy all the string with the new...
3
by: Ekhaat | last post by:
Hi I followed the Web Matrix guided tour and came to the "ASP.NET Pages with Data (Microsoft Access)" part. There is really not much you can do wrong there, but for some reason, the INSERT...
17
by: NuB | last post by:
I have a sql query that is doing an update of records, how can I add NULL to the field in the database if the field on my screen is blank? example: I have 5 textboxes, and a user can leave some...
9
by: anachronic_individual | last post by:
Hi all, Is there a standard library function to insert an array of characters at a particular point in a text stream without overwriting the existing content, such that the following data in...
1
by: cricrin | last post by:
Hello guys! This is Cristian From Argentina, and I wanted to ask you some help, I've looking on this and it makes me mad, I found that the error is in the $content , when y try to insert the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.