Qt XmlQuery subquery attribute - c++

I am trying to parse a gpx file using QXmlQuery (Qt 5.0).
The idea is to gather all the trkpt elements, and then subquery each element in order to extract latitude, longitude, altitude...
The problem is that it seems to not recognize the attribute name, because this code works:
query.setQuery
(
"declare default element namespace \"http://www.topografix.com/GPX/1/1\";"
"declare variable $gpxFile external;"
"doc($gpxFile)//trkpt"
);
if(query.isValid())
{
QXmlResultItems trkpts;
query.evaluateTo(&trkpts);
for(QXmlItem trkpt = trkpts.next(); !trkpt.isNull(); trkpt = trkpts.next())
{
QXmlQuery childQuery;
QStringList res;
childQuery.setFocus(trkpt);
childQuery.setQuery
(
"#*/string()"
); // <------------------------- this prints out all the attributes
childQuery.evaluateTo(&res);
qDebug() << res << "\n";
}
}
while this one does not:
// ... same code as above
childQuery.setQuery
(
"#lat/string()"
); // <------------------------- this prints out only a \n
// ... same code as above
any idea about what is wrong?
Please note that if I try to gather all the #lat attributes using the "outer" query, it works as expected:
query.setQuery
(
"declare default element namespace \"http://www.topografix.com/GPX/1/1\";"
"declare variable $gpxFile external;"
"doc($gpxFile)//trkpt/#lat/string()"
);
EDIT
This is a gpx sample file:
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<gpx
version="1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.topografix.com/GPX/1/0"
xmlns:topografix="http://www.topografix.com/GPX/Private/TopoGrafix/0/2"
xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd http://www.topografix.com/GPX/Private/TopoGrafix/0/2 http://www.topografix.com/GPX/Private/TopoGrafix/0/2/topografix.xsd">
<trk>
<name>My track</name>
<desc>My track description</desc>
<trkseg>
<trkpt lat="38.919839863" lon="-121.020112049">
<ele>265.447754</ele>
<time>2003-02-05T18:19:20Z</time>
</trkpt>
<trkpt lat="38.919796947" lon="-121.020240795">
<ele>264.967041</ele>
<time>2003-02-05T18:19:20Z</time>
</trkpt>
<trkpt lat="38.919861320" lon="-121.020498287">
<ele>263.044434</ele>
<time>2003-02-05T18:19:20Z</time>
</trkpt>
<trkpt lat="38.919990066" lon="-121.020798694">
<ele>263.525146</ele>
<time>2003-02-05T18:19:20Z</time>
</trkpt>
</trkseg>
</trk>
</gpx>
EDIT
I found a workaround for this problem, by using QDomDocument instead of QXmlQuery.
The result looks like the code below (all error checks removed for sake of brevity):
QFile file = ("mytrack.gpx");
file.open(QIODevice::ReadOnly);
QDomDocument doc("mydoc");
doc.setContent(&file);
QDomElement root = doc.documentElement();
QDomNodeList trkpts = root.elementsByTagName("trkpt");
for(int i = 0; i < trkpts.length(); i++)
{
QDomElement e = trkpts.at(i).toElement();
QString latitude = e.attribute("lat");
QString longitude = e.attribute("lon");
QDomNodeList elevation_list = e.elementsByTagName("ele");
QString elevation = elevation_list.at(0).toElement().text();
// ... and so on
}
I would like to find a solution to this problem by using QXmlQuery, but in the meantime this just works.

I am not sure, because I don't know QXmlQuery and how it handles query parsing and execution. But I guess it could be a namespace issue. In the first query you define a default namespace, while in the second one you do not. Maybe this default namespace is not used in the second query (childQuery). At least it would fit to #*/String() printing something. Maybe you could try if #*:lat/string works as expected.
Otherwise, some example data would be very valuable.

Related

how to write data TINYXML2 on IOS

My test.xml like this:
<?xml version="1.0"?>
<!DOCTYPE PLAY SYSTEM "play.dtd">
<data>
<CurrentLevel>5</CurrentLevel>
<BestScoreLV1>1</BestScoreLV1>
<BestScoreLV2>2</BestScoreLV2>
</data>
<dict/>
My Code here:
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("text.xml");
tinyxml2::XMLDocument doc;
doc.LoadFile(fullPath.c_str());
tinyxml2::XMLElement* ele = doc.FirstChildElement("data")->FirstChildElement("BestScoreLV2")->ToElement();
ele->SetAttribute("value", 10);
doc.SaveFile(fullPath.c_str());
const char* title1 = doc.FirstChildElement("data")->FirstChildElement("BestScoreLV2")->GetText();
int level1 = atoi(title1);
CCLOG("result is: %d",level1);
But value of BestScoreLV2 when output is also 2. How can I change and write data to XML?
In TinyXML2 text is represented by XMLText class which is child of XMLNode class.
XMLNode have methods Value() and SetValue() which have different meanings for different XML nodes.
For text nodes Value() read node's text and SetValue() write it.
So you need code like this:
tinyxml2::XMLNode* value = doc.FirstChildElement("data")->
FirstChildElement("BestScoreLV2")->FirstChild();
value->SetValue("10");
The first child of BestScoreLV2 element is XMLText with value 2. You change this value to 10 by calling SetValue(10).

TinyXML2 query text if attribute matches

I am trying to figure out a way to load the text from an XML document I have created using TinyXML2. Here is the entire document.
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" width="15" height="13" tilewidth="32" tileheight="32">
<tileset firstgid="1" name="Background" tilewidth="32" tileheight="32">
<image source="background.png" width="64" height="32"/>
</tileset>
<tileset firstgid="3" name="Block" tilewidth="32" tileheight="32">
<image source="block.png" width="32" height="32"/>
</tileset>
<layer name="Background" width="15" height="13">
<data encoding="base64">
AgAAAAIAAAACAAAA...
</data>
</layer>
<layer name="Block" width="15" height="13">
<data encoding="base64">
AwAAAAMAAAADAAAAAwAAAAM...
</data>
</layer>
</map>
Basically, I want to copy the text from <data> into a string called background only if the layer name is "Background".
I have gotten the other variables like so:
// Get the basic information about the level
version = doc.FirstChildElement("map")->FloatAttribute("version");
orientation = doc.FirstChildElement("map")->Attribute("orientation");
mapWidth = doc.FirstChildElement("map")->IntAttribute("width");
mapHeight = doc.FirstChildElement("map")->IntAttribute("height");
That works great because I know the element name and the attribute name. Is there a way to say get the doc.FirstChildElement("map")->FirstChildElement("layer") and if it == "Background", get the text.
How would I accomplish this?
I know this thread is quite old, but just in case someone perusing the internet might stumble upon this question as I have, I wish to point out that Xanx's answer can be simplified slightly.
In tinyxml2.h it says that for the function const char* Attribute( const char* name, const char* value=0 ) const, if the value parameter is not null, then the function only returns if value and name match. According to the comments in the file this:
if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar();
can be written like this:
if ( ele->Attribute( "foo" ) ) {
if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
}
So the code Xanx provided can be rewritten like this:
XMLElement * node = doc.FirstChildElement("map")->FirstChildElement("layer");
std::string value;
if (node->Attribute("name", "Background")) // no need for strcmp()
{
value = node->FirtChildElement("data")->GetText();
}
A minor change, yes, but something I wanted to add.
I advice you to do something like this:
XMLElement * node = doc.FirstChildElement("map")->FirstChildElement("layer");
std::string value;
// Get the Data element's text, if its a background:
if (strcmp(node->Attribute("name"), "Background") == 0)
{
value = node->FirtChildElement("data")->GetText();
}
auto bgData = text (find_element (doc, "map/layer[#name='Background']/data"));
Using tinyxml2 extension (#include <tixml2ex.h>).
N.B. should really be wrapped in a try/catch block.
Work in progress and documentation is incomplete (can deduce from the test example until it's ready).
I'll mention in passing that the other two answers only work properly when the desired <layer> element appears first.

Runtime error with tinyXML element access

yester day was my first attempt. I am trying to catch the variable "time" in the following "new.xml" file
<?xml version="1.0" standalone=no>
<main>
<ToDo time="1">
<Item priority="1"> Go to the <bold>Toy store!</bold></Item>
<Item priority="2"> Do bills</Item>
</ToDo>
<ToDo time="2">
<Item priority="1"> Go to the Second<bold>Toy store!</bold></Item>
</ToDo>
</main>
Here is my code
TiXmlDocument doc("new.xml");
TiXmlNode * element=doc.FirstChild("main");
element=element->FirstChild("ToDo");
string temp=static_cast<TiXmlElement *>(element)->Attribute("time");
But I am getting run time errors from the third and fourth lines. Can anybody shed a light on this isssue?
It seems to me that you forgot to load the file. Normally I do something along these lines:
TiXmlDocument doc("document.xml");
bool loadOkay = doc.LoadFile(); // Error checking in case file is missing
if(loadOkay)
{
TiXmlElement *pRoot = doc.RootElement();
TiXmlElement *element = pRoot->FirstChildElement();
while(element)
{
string value = firstChild->Value(); // In your example xml file this gives you ToDo
string attribute = firstChild->Attribute("time"); //Gets you the time variable
element = element->NextSiblingElement();
}
}
else
{
//Error conditions
}
Hope this helps
#include "tinyXml/tinyxml.h"
const char MY_XML[] = "<?xml version='1.0' standalone=no><main> <ToDo time='1'> <Item priority='1'> Go to the <bold>Toy store!</bold></Item> <Item priority='2'> Do bills</Item> </ToDo> <ToDo time='2'> <Item priority='1'> Go to the Second<bold>Toy store!</bold></Item> </ToDo></main>";
void main()
{
TiXmlDocument doc;
TiXmlHandle docHandle(&doc);
const char * const the_xml = MY_XML;
doc.Parse(MY_XML);
TiXmlElement* xElement = NULL;
xElement = docHandle.FirstChild("main").FirstChild("ToDo").ToElement();
int element_time = -1;
while(xElement)
{
if(xElement->QueryIntAttribute("time", (int*)&element_time) != TIXML_SUCCESS)
throw;
xElement = xElement->NextSiblingElement();
}
}
That's how it works. Compiled & tested.
As you can see your tries to make it extra-safe code cost you with an exceotion at your third line (of the question), and without testing I can bet it's a "pointing-to-null" exception.
Just load it my style, as TinyXml's docs say as well: "docHandle.FirstChild("main").FirstChild("ToDo").ToElement();".
Hope it helps you understand, let me know if it's not clear. I accept visa (:
Is it just me or the the pugixml version looks much better?
#include <iostream>
#include "pugixml.hpp"
using namespace std;
using namespace pugi;
int main()
{
xml_document doc;
if (!doc.load_file("new.xml"))
{
cerr << "Could not load xml";
return 1;
}
xml_node element = doc.child("main");
element = element.child("ToDo");
cout << "Time: " << element.attribute("time") << endl;
}
Also new.xml had an error, instead of:
<?xml version="1.0" standalone=no>
should be
<?xml version="1.0" standalone="no"?>
Compilation was just a matter of cl test.cpp pugixml.cpp

C++ XML parser QT - not working

Below is my code & xml file. I am trying to read a xml file and store the contents. But its not working. Please help
int ParseConfigFile::populateUserValues ( string &OS, string &dir, string &logPath )
{
QDomDocument doc;
QDomElement element;
QFile file("config_v1.xml");
if ( doc.setContent( file.readAll() ) == false )
return 1;
element = doc.documentElement();
QDomNodeList list = element.childNodes();
QDomElement firstChild = list.at(0).toElement(); // will return the first child
QDomElement secondChild = list.at(1).toElement();
QDomElement thirdChild = list.at(2).toElement();
QString s1,s2,s3;
s1 = firstChild.text();
s2 = secondChild.text();
s3 = thirdChild.text();
OS = s1.toStdString();
dir = s2.toStdString();
logPath = s3.toStdString();
return 0;
}
and my XML
<?xml version="1.0"?>
<config>
<type>LINUX</type>
<dir>/home/</dir>
<path>/var/log/</path>
</config>
You should instantiate the QDomDocument like this and then your code will work:
if ( doc.setContent(&file) == false )
return 1;
QDomDocument can work with an IO device (like QFile) making this the optimum way to set the QDomDocument's content from a file.
Anyway, if you're trying to write a configuration file reader/writer I suggest you stick with QSettings
QSettings settings("MyCompany", "MyApp");
QString s1,s2,s3;
s1 = settings.value("type").toString();
s2 = secondChild..value("dir").toString();
s3 = thirdChild..value("path").toString();
The configuration file format won't be XML (it will be key=value) but you will hardly ever need to worry about the file itself. To set any value on the file is also easy:
QSettings settings("MyCompany", "MyApp");
settings.setValue("path", "/var/log/");
Your configuration file will look like this:
[General]
path=/var/log/
See: QSettings documentation

How do I run XPath queries in QT?

How do I run an XPath query in QT?
I need to sort out certain tags with specific values in a certain attribute. The QXmlQuery documentation is anything but legible.
The schema I'm parsing is the Rhythmbox DB format:
<rhythmdb version="1.6">
<entry type="ignore">
<title></title>
<genre></genre>
<artist></artist>
<album></album>
<location>file:///mnt/disk/music/Cover.jpg</location>
<mountpoint>file:///mnt/disk</mountpoint>
<mtime>1222396828</mtime>
<date>0</date>
<mimetype>application/octet-stream</mimetype>
<mb-trackid></mb-trackid>
<mb-artistid></mb-artistid>
<mb-albumid></mb-albumid>
<mb-albumartistid></mb-albumartistid>
<mb-artistsortname></mb-artistsortname>
</entry>
<entry type="song">
<title>Bar</title>
<genre>Foobared Music</genre>
<artist>Foo</artist>
<album>The Great big Bar</album>
<track-number>1</track-number>
<disc-number>1</disc-number>
<duration>208</duration>
<file-size>8694159</file-size>
<location>file:///media/disk/music/01-Foo_-_Bar.ogg
<mountpoint>file:///media/disk
<mtime>1216995840</mtime>
<first-seen>1250478814</first-seen>
<last-seen>1250478814</last-seen>
<bitrate>301</bitrate>
<date>732677</date>
<mimetype>application/x-id3</mimetype>
<mb-trackid></mb-trackid>
<mb-artistid></mb-artistid>
<mb-albumid></mb-albumid>
<mb-albumartistid></mb-albumartistid>
<mb-artistsortname></mb-artistsortname>
</entry>
</rhythmdb>
This is your basic XML Schema which has a collection of structured entries. My intention was to filter out the entries with the type 'ignore'.
The relevant documentation is at: http://qt-project.org/doc/qt-4.8/qxmlquery.html#running-xpath-expressions.
The solution I came to was to use QXmlQuery to generate an XML file then parse it again using QDomDocument.
RhythmboxTrackModel::RhythmboxTrackModel()
{
QXmlQuery query;
QXmlQuery entries;
QString res;
QDomDocument rhythmdb;
/*
* Try and open the Rhythmbox DB. An API call which tells us where
* the file is would be nice.
*/
QFile db(QDir::homePath() + "/.gnome2/rhythmbox/rhythmdb.xml");
if ( ! db.exists()) {
db.setFileName(QDir::homePath() + "/.local/share/rhythmbox/rhythmdb.xml");
if ( ! db.exists())
return;
}
if (!db.open(QIODevice::ReadOnly | QIODevice::Text))
return;
/*
* Use QXmlQuery to execute and XPath query. Check the version to
* make sure.
*/
query.setFocus(&db);
query.setQuery("rhythmdb[#version='1.6']/entry[#type='song']");
if ( ! query.isValid())
return;
query.evaluateTo(&res);
db.close();
/*
* Parse the result as an XML file. These shennanigans actually
* reduce the load time from a minute to a matter of seconds.
*/
rhythmdb.setContent("" + res + "");
m_entryNodes = rhythmdb.elementsByTagName("entry");
for (int i = 0; i < m_entryNodes.count(); i++) {
QDomNode n = m_entryNodes.at(i);
QString location = n.firstChildElement("location").text();
m_mTracksByLocation[location] = n;
}
qDebug() << rhythmdb.doctype().name();
qDebug() << "RhythmboxTrackModel: m_entryNodes size is" << m_entryNodes.size();
}
In case anyone is wondering, this is my code taken from a recent branch of the Mixxx project, specifically the features_looping branch.
The things I dislike about this solution are:
Parsing the XML twice
Concatenating the result with a starting and ending tag.
If it fits your parsing requirements, you can use the SAX-based reader instead of a DOM-based one. Using QXmlSimpleReader with a sub-classed QXmlDefaultHandler, you can get access to each element of your XPath query as well as its attributes as the document is scanned. I think this approach would be faster than a DOM-based one; you don't have to read anything twice and it's already built into Qt. There is an example here: http://www.digitalfanatics.org/projects/qt_tutorial/chapter09.html under "Reading Using SAX."