TXMLDocument usage & formatting question


2003-07-03 11:36:43 PM
cppbuilder106
Hi,
I'm using the TXMLDocument component in BCB6 Pro reasonably
successfully but am stumped on a formatting issue.
At the mo this is what code i'm using to generate the accompanying
xml:
#include file
_di_IXMLDocument XMLDoc;
_di_IXMLNode XMLParentNode;
_di_IXMLNode XMLChild;
_di_IXMLNode XMLChildNode;
#cpp file
void __fastcall TfrmXML::Button1Click(TObject *Sender)
{
XMLDoc = NewXMLDocument();
if(XMLDoc)
{
XMLDoc->Active = true;
XMLDoc->Encoding = "UTF-8";
XMLDoc->NodeIndentStr = "<2 spaces>";
String szDate = DateToStr(Now());
XMLDoc->DocumentElement = XMLDoc->CreateNode("MACHINENAME",
ntElement, szDate);
XMLParentNode = XMLDoc->DocumentElement;
if(XMLParentNode)
{
XMLChildNode = XMLParentNode;
if(XMLChildNode)
{
// environmental
AddXMLData(XMLChildNode, "RegisterOrg",
Audit->GetRegOrg());
AddXMLData(XMLChildNode, "RegisterOwner",
Audit->GetRegOwner());
AddXMLData(XMLChildNode, "WindowsDir",
Audit->GetWinDir());
AddXMLData(XMLChildNode, "SystemDir",
Audit->GetSysDir());
AddXMLData(XMLChildNode, "TempDir",
Audit->GetTempDir());
AddXMLData(XMLChildNode, "User", Audit->GetUser());
}
XMLChildNode = XMLParentNode;
if(XMLChildNode)
{
// memory
AddXMLData(XMLChildNode, "TotalPhysicalMemory",
Audit->GetTotalPhysicalMemory());
AddXMLData(XMLChildNode, "VirtualMemFree",
Audit->GetVirtualMemFree());
AddXMLData(XMLChildNode, "VirtualMemTotal",
Audit->GetVirtualMemTotal());
AddXMLData(XMLChildNode, "PagingFileFree",
Audit->GetPagingFileFree());
AddXMLData(XMLChildNode, "PagingFileTotal",
Audit->GetPagingFileTotal());
AddXMLData(XMLChildNode, "FreePhysicalMemory",
Audit->GetFreePhysicalMemory());
}
}
XMLDoc->SaveToFile("test.xml");
XMLDoc->Active = false;
}
}
//---------------------------------------------------------------------------
// this method simplifies the adding of data to our xml string
// returns 0 if ok, 1 if failed
int __fastcall TfrmXML::AddXMLData(_di_IXMLNode XMLNodeToAdd, String
sChildData, String ChildText)
{
XMLChild = XMLNodeToAdd->AddChild(sChildData);
if(XMLChild)
{
XMLChild->Text = ChildText;
return 0;
}
else
return 1;
}
& this is what it's outputting:
<?xml version="1.0" encoding="UTF-8" ?>
<MACHINENAME xmlns="03/07/2003">
<RegisterOrg>Company name</RegisterOrg>
<RegisterOwner>Owner name</RegisterOwner>
<WindowsDir>C:\WINNT</WindowsDir>
<SystemDir>C:\WINNT\system32</SystemDir>
<TempDir>C:\WINNT\Profiles\si\LOCALS~1\Temp\</TempDir>
<User>Si</User>
<TotalPhysicalMemory>261616 Kbytes</TotalPhysicalMemory>
<VirtualMemFree>1f5860 Kbytes</VirtualMemFree>
<VirtualMemTotal>1fff80 total Kbytes</VirtualMemTotal>
<PagingFileFree>Paging file : 258388 free Kbytes</PagingFileFree>
<PagingFileTotal>632356 Kbytes</PagingFileTotal>
<FreePhysicalMemory>48768 Kbytes</FreePhysicalMemory>
</MACHINENAME>
Question is how can I get it outputing in the following format?
<?xml version="1.0" encoding="UTF-8" ?>
<MACHINENAME xmlns="03/07/2003">
<COMPANY>
<RegisterOrg>Company name</RegisterOrg>
<RegisterOwner>Owner name</RegisterOwner>
<WindowsDir>C:\WINNT</WindowsDir>
<SystemDir>C:\WINNT\system32</SystemDir>
<TempDir>C:\WINNT\Profiles\si\LOCALS~1\Temp\</TempDir>
<User>Si</User>
</COMPANY>
<MEMORY>
<TotalPhysicalMemory>261616 Kbytes</TotalPhysicalMemory>
<VirtualMemFree>1f5860 Kbytes</VirtualMemFree>
<VirtualMemTotal>1fff80 total Kbytes</VirtualMemTotal>
<PagingFileFree>Paging file : 258388 free
Kbytes</PagingFileFree>
<PagingFileTotal>632356 Kbytes</PagingFileTotal>
<FreePhysicalMemory>48768 Kbytes</FreePhysicalMemory>
</MEMORY>
</MACHINENAME>
I've tried :
replacing
XMLChildNode = XMLParentNode;
with
XMLDoc->DocumentElement = XMLDoc->CreateNode("Memory", ntElement,
szDate);
XMLParentNode = XMLDoc->DocumentElement;
But this just generates the following:
<?xml version="1.0" encoding="UTF-8" ?>
<MEMORY>
<TotalPhysicalMemory>261616 Kbytes</TotalPhysicalMemory>
<VirtualMemFree>1f5860 Kbytes</VirtualMemFree>
<VirtualMemTotal>1fff80 total Kbytes</VirtualMemTotal>
<PagingFileFree>Paging file : 258388 free
Kbytes</PagingFileFree>
<PagingFileTotal>632356 Kbytes</PagingFileTotal>
<FreePhysicalMemory>48768 Kbytes</FreePhysicalMemory>
</MEMORY>
Help!!!
Si