Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » how to check specific end tag in a xml file?
how to check specific end tag in a xml file? [message #35785] Wed, 21 March 2012 11:57 Go to next message
ayana is currently offline  ayana
Messages: 16
Registered: November 2011
Promising Member
Hi All,

How to check for a specific end tag in the large XML file while parsing it?

Suppose the file is,

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>

<!DOCTYPE AddressBook>

<AddressBook>

<person>

<name>qw</name>

<surname>we</surname>

<address>we</address>

<email>er</email>

</person>

<person>

<name>rer</name>

<surname>ty</surname>

<address>ui</address>

<email>io</email>

</person>

<person>

<name>oo</name>

<surname>jlj</surname>

<address>bh</address>

<email>cft</email>

</person>

</AddressBook>

The user enters a name in an edit field of GUI, if the name entered matches with the gathered text of 'name tag' the corresponding person details should get add to XMLTree.

For Example:
while(! p.End)
{

if ( p.Tag ( "person" ))

{

if ( p.Tag ( "person" ))

{
if ( p.Tag ( "name" ) )

{

if( editfield.name==p.ReadText())
{
Add that "<person> to </person>" details to XmlTree
}


}


}


}


}



Thanks in Advance,


-Ayana



Re: how to check specific end tag in a xml file? [message #35799 is a reply to message #35785] Thu, 22 March 2012 08:07 Go to previous messageGo to next message
ayana is currently offline  ayana
Messages: 16
Registered: November 2011
Promising Member
Hi,

Please help me to solve the above problem...

Otherwise, I want to save each person details into different xml files. is that possible for the above AddressBook XML file, if so how?

Regards,

Ayana
Re: how to check specific end tag in a xml file? [message #35802 is a reply to message #35785] Thu, 22 March 2012 10:45 Go to previous messageGo to next message
Sender Ghost is currently offline  Sender Ghost
Messages: 301
Registered: November 2008
Senior Member
Hello, Ayana.

ayana wrote on Wed, 21 March 2012 11:57

How to check for a specific end tag in the large XML file while parsing it?


ayana wrote on Wed, 21 March 2012 11:57

The user enters a name in an edit field of GUI, if the name entered matches with the gathered text of 'name tag' the corresponding person details should get add to XMLTree.


There is XML reference example about methods to parse XML files with XmlParser and XmlNode. As well as AddressBookXML and AddressBookXML2 examples.

I will show you following source code about how to search "name" tag for specified names and changing found "person" data, using XmlNode:
Toggle Spoiler

With following XML file:
Toggle Spoiler

You will get following output:
Toggle Spoiler

and changed XML file:
Toggle Spoiler

[Updated on: Thu, 22 March 2012 12:46]

Report message to a moderator

Re: how to check specific end tag in a xml file? [message #35829 is a reply to message #35802] Tue, 27 March 2012 06:49 Go to previous messageGo to next message
ayana is currently offline  ayana
Messages: 16
Registered: November 2011
Promising Member
Hi Sender,

Thanks for your reply.

But, i want to save each person to person tag details into individual XML files in the name of "gathered text of 'name' tag".

Example:

<Xml>
<UUID>f9904fb0dacd1661dc3358c69c032bca</UUID>

<Project_Information>
<ProjectName>erwerw</ProjectName>
<Description>dfg</Description>
<Date>27/03/2012</Date>
</Project_Information>

<InputDataInformation>

<Information>
<Name>Info1</Name>
<Type>jj</Type>
<DataType>fty</DataType>
<Size>45</Size>
<Unit>qerw</Unit>
<Scale>4455</Scale>
</Information>

<Information>
<Name>Info2</Name>
<Type>ww</Type>
<DataType>fer</DataType>
<Size>24</Size>
<Unit>qhy</Unit>
<Scale>1234</Scale>
</Information>

<InputDataInformation>

</Xml>

in the above xml file i want to save each information details to save into two individual xml files, in the name of "gathered text of 'Name' tag".
Like, Info1.xml and Info2.xml

Inside Info1.xml file, i want the following to be get saved

<Information>
<Name>Info1</Name>
<Type>jj</Type>
<DataType>fty</DataType>
<Size>45</Size>
<Unit>qerw</Unit>
<Scale>4455</Scale>
</Information>

Inside Info2.xml file,

<Information>
<Name>Info2</Name>
<Type>ww</Type>
<DataType>fer</DataType>
<Size>24</Size>
<Unit>qhy</Unit>
<Scale>1234</Scale>
</Information>


With Regards,

Ayana
Re: how to check specific end tag in a xml file? [message #35834 is a reply to message #35829] Tue, 27 March 2012 13:22 Go to previous messageGo to next message
ayana is currently offline  ayana
Messages: 16
Registered: November 2011
Promising Member
Hello All,

Please help me to accomplish above task.

Regards,

Ayana
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 Go to previous message
Sender Ghost is currently offline  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 207 times)
Previous Topic: ParseXML intermittent crash
Next Topic: bug in http.cpp
Goto Forum:
  


Current Time: Thu Mar 28 12:03:29 CET 2024

Total time taken to generate the page: 0.02403 seconds