QT中使用QXmlStreamReader解析XML文件
XML文件由于其扩展性与兼容性的优点,被广泛用作软件和系统的配置文件。这里简要介绍一下QT下如何来解析XML文件。
源代码:
xml_reader.h
#ifndef XML_READER_H
#define XML_READER_H
#include <QtCore>
//#include <QtGui>
class xml_reader : public QXmlStreamReader
{
//Q_OBJECT
public:
xml_reader();
bool read(QIODevice *device);
private:
void read_data();
void read_startweek();
void read_eventnumber();
void read_period();
void read_id();
void read_starttime();
void read_endtime();
void read_exception();
void read_subexception();
void read_description();
void read_duration();
void read_event();
void read_offsettime();
void read_eventdesc();
void read_eventname();
void read_prompt();
void read_lasthandle();
void read_unknow();
};
#endif // XML_READER_H
xml_reader.cpp
#include "xml_reader.h"
xml_reader::xml_reader()
{}
bool xml_reader::read(QIODevice *device)
{
setDevice(device);
while(!atEnd())
{
readNext();
if(isStartElement())
{
if(name() == "data")
{
read_data();
}
}
}
return !error();
}
void xml_reader::read_data()
{
qDebug()<<"Start to read data...";
while(!atEnd())
{
readNext();
if(isStartElement())
{
if(name() == "period")
{
read_period();
}
if(name() == "startweek")
{
read_startweek();
}
if(name() == "eventnumber")
{
read_eventnumber();
}
}
if(isEndElement())
{
if(name() == "data")
{
qDebug()<<"Now at the end of data element...";
break;
}
}
}
}
void xml_reader::read_startweek()
{
相关文档:
xml:
<?xml version="1.0" encoding="utf-8" ?>
<library>
<name>首都图书馆</name>
<address>朝阳区华威桥南</address>
<books>
<book type="math">
<id>0000</id>
</book&g ......
<Records>
<Record>
<id>1 </id>
<name>李四 </name>
</Record>
<Record>
<id>2 </id>
<name>张三 </name>
</Record>
<Record>
<id>3 </id>
<name>王五 </name>
</Record& ......
Flash中的XML对象也有方法可以将XML数据向服务器发送。即send和sendAndLoad。send方法向某个 URL 传递 XML 对象,返回的信息都被发送到另外一个浏览器窗口;sendAndLoad 方法向某个 URL 发送一个 XML 对象。返回的信息都放在一个XML对象中。
在某种程度上, XML的send方法和sendAndLoad方法类似于loadVars对象的send方法和 ......
Class.xml
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="class.css"?>
<bookdetail>
<book class="A">
<author>曹雪芹</author>
<title>红楼梦</title>
<price>60.00</price>
</book>
<book class="A"& ......
xmlpost by HttpWebRequest:
protected string PostXmlToURL(string url,string data)
{
HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(url);
hwr.Method = "POST";
Stream stream = hwr.GetRequestStream();
StreamWri ......