Hi everybody !
I'm developping a program that reads a XML file and gets its data using SAX2. It works quite well, the function QXmlContentHandler::characters(const QString& ch) of my handler is called for each content between element but also at each line end ... maybe it takes care of the carriage return and/or line feed specific character ? Does someone have the same problem ?
Here is the content of my XML file (I have edited it using wordpad under Windows) :
<?xml version="1.0" encoding="ISO-8859-1"?>
<phonebook>
<person>
<type>Worker</type>
<name>NAME_1</name>
<firstname>FIRST_NAME_1</firstname>
<phone>0123456789</phone>
</person>
<person>
<type>Student</type>
<name>NAME_2</name>
<firstname>FIRST_NAME_2</firstname>
<phone>9876543210</phone>
</person>
</phonebook>
<?xml version="1.0" encoding="ISO-8859-1"?>
<phonebook>
<person>
<type>Worker</type>
<name>NAME_1</name>
<firstname>FIRST_NAME_1</firstname>
<phone>0123456789</phone>
</person>
<person>
<type>Student</type>
<name>NAME_2</name>
<firstname>FIRST_NAME_2</firstname>
<phone>9876543210</phone>
</person>
</phonebook>
To copy to clipboard, switch view to plain text mode
The code of my handler is :
bool CMySaxContentHandler::startElement(const QString& namespaceURI, const QString& localName,
const QString& qName, const QXmlAttributes& attribute)
{
szIndent += " ";
szMessage = szIndent + "<" + qName + ">";
qDebug("%s", szMessage.ascii());
return true;
}
bool CMySaxContentHandler::endElement(const QString& namespaceURI, const QString& localName, const QString& qName)
{
szMessage = szIndent + "</" + qName + ">";
qDebug("%s", szMessage.ascii());
szIndent.remove(0, 4);
return true;
}
bool CMySaxContentHandler::characters(const QString& ch)
{
szIndent += " ";
szMessage = szIndent + ch;
qDebug("%s", szMessage.ascii());
szIndent.remove(0, 4);
return true;
}
bool CMySaxContentHandler::startElement(const QString& namespaceURI, const QString& localName,
const QString& qName, const QXmlAttributes& attribute)
{
szIndent += " ";
szMessage = szIndent + "<" + qName + ">";
qDebug("%s", szMessage.ascii());
return true;
}
bool CMySaxContentHandler::endElement(const QString& namespaceURI, const QString& localName, const QString& qName)
{
szMessage = szIndent + "</" + qName + ">";
qDebug("%s", szMessage.ascii());
szIndent.remove(0, 4);
return true;
}
bool CMySaxContentHandler::characters(const QString& ch)
{
szIndent += " ";
szMessage = szIndent + ch;
qDebug("%s", szMessage.ascii());
szIndent.remove(0, 4);
return true;
}
To copy to clipboard, switch view to plain text mode
... where szIndent and szMessage are two QString members.
The code used to parse my XML file is the following :
CMySaxContentHandler handler;
xsr.setContentHandler(&handler);
xsr.parse(&xis);
QXmlSimpleReader xsr;
CMySaxContentHandler handler;
xsr.setContentHandler(&handler);
QXmlInputSource xis(QFile("test_without_dtd.xml"));
xsr.parse(&xis);
To copy to clipboard, switch view to plain text mode
and it produces the following output :
<phonebook>
<person>
<type>
Worker
</type>
<name>
NAME_1
</name>
<firstname>
FIRST_NAME_1
</firstname>
<phone>
0123456789
</phone>
</person>
<person>
<type>
Student
</type>
<name>
NAME_2
</name>
<firstname>
FIRST_NAME_2
</firstname>
<phone>
9876543210
</phone>
</person>
</phonebook>
<phonebook>
<person>
<type>
Worker
</type>
<name>
NAME_1
</name>
<firstname>
FIRST_NAME_1
</firstname>
<phone>
0123456789
</phone>
</person>
<person>
<type>
Student
</type>
<name>
NAME_2
</name>
<firstname>
FIRST_NAME_2
</firstname>
<phone>
9876543210
</phone>
</person>
</phonebook>
To copy to clipboard, switch view to plain text mode
How could I know if the parameter of the characters handler function must be process if I am not sure it is an element content or something else ?
Thanks for your help.
Bookmarks