TinyXML2 C++ Import - c++

I've got a XML File looking like:
<?xml version="1.0" encoding="UTF-16"?>
<Table>
<Dataset>
<Year>Year1</Year>
<Month>Month1</Month>
<Day>Day1</Day>
</Dataset>
<Dataset>
<Year>Year2</Year>
<Month>Month2</Month>
<Day>Day1</Day>
</Dataset>
</Table>
And I want to read this file with C++. My code looks like:
XMLElement* xeTable = xeExport->FirstChildElement("Table");
XMLElement* xeDataset = xeTable->FirstChildElement("Dataset");
XMLElement* xeYear = xeDataset->FirstChildElement("Year");
XMLElement* xeMonth = xeDataset->FirstChildElement("Month");
XMLElement* xeDay = xeDataset->FirstChildElement("Day");
XMLText* xnYear = xeYear->FirstChild()->ToText();
const char* cYear = xnYear->Value();
XMLText* xnMonth = xeMonth->FirstChild()->ToText();
const char* cMonth = xnMonth->Value();
XMLText* xnDay = xeDay->FirstChild()->ToText();
const char* cDay = xnDay->Value();
It reads year, month and date of first dataset. What to do know, for reading data of next dataset? I superiored to delete the first dataset after reading so I can read the second dataset again with FirstChildElement();. But I didn't get it.
Can anyone help?

xeDataset = xeDataset->NextSiblingElement("Dataset")
to elaborate:
XMLElement* xeTable = xeExport->FirstChildElement("Table");
for(XMLElement* xeDataset = xeTable->FirstChildElement("Dataset"); xeDataset; xeDataset = xeDataset->NextSiblingElement("Dataset"))
{
XMLElement* xeYear = xeDataset->FirstChildElement("Year");
XMLElement* xeMonth = xeDataset->FirstChildElement("Month");
XMLElement* xeDay = xeDataset->FirstChildElement("Day");
XMLText* xnYear = xeYear->FirstChild()->ToText();
const char* cYear = xnYear->Value();
XMLText* xnMonth = xeMonth->FirstChild()->ToText();
const char* cMonth = xnMonth->Value();
XMLText* xnDay = xeDay->FirstChild()->ToText();
const char* cDay = xnDay->Value();
}

Related

Unable to call SecItemAdd Successfully

I'm trying to add a simple string secret to they keychain in macOS, via the C++ APIs. Unfortunately, I can't get my call to SecItemAdd to work. I know it is because my value for the kSecValueRef key is the wrong type, but so far Google/Apple docs/existing StackOverflow questions have yet to reveal to me what type I should be using, and how to create it. Here's what I have so far:
CFStringRef keys[4];
keys[0] = kSecClass;
keys[1] = kSecValueRef;
keys[2] = kSecAttrAccount;
keys[3] = kSecAttrService;
CFTypeRef values[4];
values[0] = kSecClassGenericPassword;
values[1] = CFSTR("password");
values[2] = CFSTR( "account-1" );
values[3] = CFSTR( "service-1" );
CFDictionaryRef attributes = CFDictionaryCreate
(
( CFAllocatorRef )NULL,
( const void ** )keys,
( const void ** )values,
4,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks
);
CFShow(attributes);
OSStatus status = SecItemAdd(attributes, NULL);
So what should I be putting for my kSecValueRef? I've tried making a reference to a CFStringRef, but that didn't work. I also see that in the Apple docs it says:
The corresponding value, depending on the item class requested, is of type SecKeychainItem, SecKey, SecCertificate, or SecIdentity.
But I don't have a SecKeychainItem because I haven't made added the item yet, and the other types don't seem right for just a normal string.
Thoughts?
Figured it out! For simple data, such as a string, you should use the kSecValueData key. That will let you just pass in a CFStringRef.
Example:
CFStringRef keys[4];
keys[0] = kSecClass;
keys[1] = kSecAttrAccount;
keys[2] = kSecAttrService;
keys[3] = kSecValueData;
CFTypeRef values[4];
values[0] = kSecClassGenericPassword;
values[1] = CFSTR( "account-15" );
values[2] = CFSTR( "service-126" );
values[3] = CFSTR( "password" );

tinyxml parsing xml file

I have a xml file like this:
<?xml version="1.0"?>
<ApplicationSettings>
<BeamGeometry
Dimension="2"
Type="fan"
Shape="arc"
LengthFocalPointToISOCenter="558"
LengthISOCenterToDetector="394"
LengthDetectorSeperation="0.98"
LengthModuleSeperation="0.04"
NumberModules="57"
NumberDetectorsPerModule="16"
NumberISOCenterShift="3.25" />
</ApplicationSettings>
And I'd like to use tinyxml retrieving all the values (such as 558) based on the entry name such as (LengthFocalPointToISOCenter). Here is my code, not successful yet.
int SetFanbeamGeometry(const char* filename)
{
int ret = TRUE;
TiXmlDocument doc("E:\\Projects\\iterativeRecon\\ProjectPackage\\ApplicationSettings\\ApplicationSettings.xml");
int LengthFocalPointToISOCenter;
if( doc.LoadFile())
{
TiXmlHandle hDoc(&doc);
TiXmlElement *pRoot, *pParm;
pRoot = doc.FirstChildElement("ApplicationSettings");
if(pRoot)
{
pParm = pRoot->FirstChildElement("BeamGeometry");
int i = 0; // for sorting the entries
while(pParm)
{
pParm = pParm->NextSiblingElement("BeamGeometry");
i++;
}
}
}
else
{
printf("Warning: ApplicationSettings is not loaded!");
ret = FALSE;
}
return ret;
}
I am wondering how can I use tinyxml to do that? Sorry I am a first time user. and it looks confusing to me. Thanks.
There's only one BeamGeometry child element in the snippet you've shown; the information you're trying to access are its attributes - they're not individual elements.
So you need something like this:
// ...
pParm = pRoot->FirstChildElement("BeamGeometry");
if (pParm)
{
const char* pAttr = pParm->Attribute("LengthFocalPointToISOCenter");
if (pAttr)
{
int iLengthFocalPointToISOCenter = strtoul(pAttr, NULL, 10);
// do something with the value
}
}
If you want to loop through all attributes, it's quite simple:
const TiXmlAttribute* pAttr = pParm->FirstAttribute();
while (pAttr)
{
const char* name = pAttr->Name(); // attribute name
const char* value = pAttr->Value(); // attribute value
// do something
pAttr = pAttr->Next();
}

Can't run execv

I've been trying to run command using exevp as follows:
char *args[11];
args[0] = (char*)lgulppath.c_str();
args[1] = (char*)"-i";
args[2] = (char*)sniffer_interface.c_str();
args[3] = (char*)"-r";
args[4] = (char*)pcapfileLimit.c_str();
args[5] = (char*)"-C";
args[6] = (char*)"1";
args[7] = (char*)"-f";
args[8] = (char*)serverip_filter.c_str();
args[9] = (char*)"-o";
args[10] = (char*)lpipepath.c_str();
execv("/usr/sbin/program",args);
this works. HOWEVER, when I want to have the first parameter "/usr/sbin/program" as a parameter say:
string str = "/usr/sbin/program";
//char* args is assigned as above
execv(str.c_str(),args);
this fails and returns -1. I CAN'T GET WHY THOUGH.
Thanks everybody
Null terminate the arguments you pass to execv. Something like
char *args[12];
// other args..
args[11] = (char*) 0;

TinyXML2 and C++

I want to read the data from a wxGrid and write it into a XML File.
The wxGrid is like:
Jahr | Monat
------ |-------------
2012 | 03
2009 | 08
What I want to have:
<SQL>
<Datensatz>
<Jahr>2012</Jahr>
<Monat>03</Monat>
</Datensatz>
<Datensatz>
<Jahr>2009</Jahr>
<Monat>08</Monat>
</Datensatz>
</SQL>
What I got:
<SQL>
<Datensatz>
<Jahr>20122009</Jahr>
<Monat>0308</Monat>
</Datensatz>
<Datensatz>
<Jahr>20122009</Jahr>
<Monat>0308</Monat>
</Datensatz>
</SQL>
My Code:
XMLDocument doc;
XMLElement* xesql = doc.NewElement("SQL");
XMLNode * xnsql = doc.InsertFirstChild(xesql);
XMLElement* xejahr = doc.NewElement("Jahr");
XMLElement* xemonat = doc.NewElement("Monat");
XMLText* datensatzJahr = doc.NewText("");
XMLText* datensatzMonat = doc.NewText("");
for(int i=0; i<=1; i++)
{
XMLElement* xedatensatz = doc.NewElement("Datensatz");
datensatzJahr = doc.NewText(m_gd_data->GetCellValue(i,0));
datensatzMonat = doc.NewText(m_gd_data->GetCellValue(i,1));
xejahr->InsertEndChild(datensatzJahr);
xemonat->InsertEndChild(datensatzMonat);
xedatensatz->InsertEndChild(xejahr);
xedatensatz->InsertEndChild(xemonat);
xesql->InsertEndChild(xedatensatz);
}
doc.SaveFile(path);
I really don't know where's the problem. Can anyone help?
You are not resetting the XML elements for each iteration of the loop, hence you are only appending text to an existing element. this should work:
XMLDocument doc;
XMLElement* xesql = doc.NewElement("SQL");
XMLNode * xnsql = doc.InsertFirstChild(xesql);
for(int i=0; i<=1; i++)
{
XMLElement* xejahr = doc.NewElement("Jahr");
XMLElement* xemonat = doc.NewElement("Monat");
XMLText* datensatzJahr = doc.NewText("");
XMLText* datensatzMonat = doc.NewText("");
XMLElement* xedatensatz = doc.NewElement("Datensatz");
datensatzJahr = doc.NewText(m_gd_data->GetCellValue(i,0));
datensatzMonat = doc.NewText(m_gd_data->GetCellValue(i,1));
xejahr->InsertEndChild(datensatzJahr);
xemonat->InsertEndChild(datensatzMonat);
xedatensatz->InsertEndChild(xejahr);
xedatensatz->InsertEndChild(xemonat);
xesql->InsertEndChild(xedatensatz);
}
doc.SaveFile(path);
You have to create new elements for the year and month inside the loop.

Extract XML embedded in another XML using PugiXML

Is there an easy way in PugiXML to unescape and load an XML embedded in another XML.
Here it is
xml_parse_result pr = doc.load_buffer_inplace( (void * )response.c_str(), response.size() );
if (pr.status!=status_ok)
return pr;
xml_node resultXml = doc.child("soap:Envelope");
resultXml = resultXml.child("soap:Body");
resultXml = resultXml.child( (webmethodName + "Response").c_str() );
resultXml = resultXml.child( (webmethodName + "Result").c_str() );
pr = doc.load( resultXml.first_child().value() );
return pr;