Home » U++ Library support » U++ Core » how to check specific end tag in a xml file?
Re: how to check specific end tag in a xml file? [message #35839 is a reply to message #35829] |
Tue, 27 March 2012 17:37 |
Sender Ghost
Messages: 301 Registered: November 2008
|
Senior Member |
|
|
ayana wrote on Tue, 27 March 2012 06:49 | But, I want to save each person to person tag details into individual XML files in the name of "gathered text of 'name' tag".
|
ayana wrote on Tue, 27 March 2012 13:22 | Please help me to accomplish above task.
|
What you asked for is already inside examples. This is general examples, which not adapted for your specific case, of course.
There is following source code to accomplish your task (in some way):
#include <Core/Core.h>
using namespace Upp;
CONSOLE_APP_MAIN
{
const Vector<String>& cmdLine = CommandLine();
if (cmdLine.GetCount() == 0) {
Cout() << "Specify XML file to load\n";
return;
}
String fileName(NormalizePath(cmdLine[0]));
XmlNode node;
{
// Loading contents of file to String
const String data = LoadFile(fileName);
if (data.IsVoid()) {
Cerr() << "Error, while loading '" << NormalizePath(fileName) << "' file\n";
SetExitCode(1);
return;
}
try { // Trying to parse XML data
node = ParseXML(data);
}
catch (XmlError e) {
Cerr() << "XmlError: " << e << '\n';
SetExitCode(1);
return;
}
}
const XmlNode& linput = node["Xml"]["InputDataInformation"];
if (linput.IsVoid()) {
Cerr() << "There is no Xml/InputDataInformation tag\n";
SetExitCode(1);
return;
}
const String outputPath = "output";
if (!DirectoryExists(outputPath))
if (!RealizeDirectory(outputPath)) {
Cerr() << "Error while creating '" << outputPath << "' directory\n";
SetExitCode(1);
return;
}
for (int i = 0, n = linput.GetCount(); i < n; ++i) {
const XmlNode& linfo = linput[i];
if (!linfo.IsTag("Information"))
continue;
const XmlNode& lname = linfo["Name"];
if (linfo.IsVoid())
continue;
const String name = lname[0].GetText();
if (name.IsEmpty()) {
Cerr() << "Empty name for " << i << " tag\n";
continue;
}
const String outputFile = AppendFileName(outputPath, name + ".xml");
if (!SaveFile(outputFile, AsXML(linfo, XML_HEADER))) {
Cerr() << "Error, while saving '" << outputFile << "' file\n";
SetExitCode(1);
return;
}
}
}
Also, you could find full package (with correct input XML file) inside attachment archive.
-
Attachment: SplitXml.zip
(Size: 1.84KB, Downloaded 214 times)
|
|
|
Goto Forum:
Current Time: Sun May 05 20:43:41 CEST 2024
Total time taken to generate the page: 0.02182 seconds
|