我正在尝试读取XML标记内的文本。我正在使用readelementtext()函数,但它返回一个空的Qstring。我使用的代码如下;
#include <QCoreApplication>
#include <QFile>
#include <QStringList>
#include <QTextStream>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include <QByteArray>
#include <iostream>
#include <ostream>
#include <QString>
class XmlStreamLint
{
public:
Q_DECLARE_TR_FUNCTIONS(XmlStreamLint)
};
int main(int argc, char *argv[])
{
enum ExitCode
{
Success,
ParseFailure,
ArgumentError,
WriteError,
FileFailure
};
QCoreApplication app(argc, argv);
QTextStream errorStream(stderr);
if (argc != 2)
{
errorStream << XmlStreamLint::tr(
"Usage: xmlstreamlint <path to XML file>\n");
return ArgumentError;
}
QString inputFilePath(QCoreApplication::arguments().at(1));
QFile inputFile(inputFilePath);
if (!QFile::exists(inputFilePath))
{
errorStream << XmlStreamLint::tr(
"File %1 does not exist.\n").arg(inputFilePath);
return FileFailure;
} else if (!inputFile.open(QIODevice::ReadOnly|QIODevice::Text)) {
errorStream << XmlStreamLint::tr(
"Failed to open file %1.\n").arg(inputFilePath);
return FileFailure;
}
QString outputfilepath="/home/shakeel/xmlstreamlint/testing.xml";
QFile outputFile(outputfilepath);
//QFile outputFile;
if (!outputFile.open(stdout, QIODevice::WriteOnly))
{
errorStream << XmlStreamLint::tr("Failed to open stdout.");
return WriteError;
}
QByteArray array[20000];
QByteArray *ptr;
ptr=&array[0];
QXmlStreamReader reader(&inputFile);
//QXmlStreamWriter writer(&outputFile);
QXmlStreamWriter writer(ptr);
QString text,signal;
int count=0;
while (!reader.atEnd())
{
reader.readNext();
if (reader.error())
{
errorStream << XmlStreamLint::tr(
"Error: %1 in file %2 at line %3, column %4.\n").arg(
reader.errorString(), inputFilePath,
QString::number(reader.lineNumber()),
QString::number(reader.columnNumber()));
return ParseFailure;
} else
if (reader.isStartElement())
{
text = reader.name().toString();
if(text.operator ==("TYPICAL_CYCLE_CHANNEL"))
{
count=count+1;
signal=reader.readElementText();
writer.writeCurrentToken(reader);
我想读的XML如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ECG SYSTEM "mortara.dtd">
<!-- Generated by Mortara ELI Link 2.20 -->
<!-- Mortara Instrument, Inc. -->
<!-- Server Name - Unknown -->
<ECG
ACQUISITION_TIME="20070227033111"
ROOM=""
LOCATION=""
COMMENT=""
AGE="59"
AGE_UNITS="Y"
HEIGHT=""
HEIGHT_UNITS="I"
WEIGHT=""
WEIGHT_UNITS="L"
NUM_QRS="10"
AVERAGE_RR="935"
VENT_RATE="64"
SEQUENCE_NUMBER="6"
>
<DEMOGRAPHIC_FIELD ID="1" LABEL="Name:" VALUE="Asd" UNITS="" />
<DEMOGRAPHIC_FIELD ID="7" LABEL="FName:&," VALUE="We" UNITS="" />
<DEMOGRAPHIC_FIELD ID="2" LABEL="ID&ID:" VALUE="12" UNITS="" />
<DEMOGRAPHIC_FIELD ID="16" LABEL="DOB:" VALUE="12/13/47" UNITS="" />
<DEMOGRAPHIC_FIELD ID="3" LABEL="Age&yr" VALUE="59" UNITS="Y" />
<DEMOGRAPHIC_FIELD ID="4" LABEL="Sex" VALUE="Male" UNITS="" />
<SITE
ID="0"
/>
<SUBJECT
LAST_NAME="Asd"
FIRST_NAME="We"
GENDER="Male"
ID="12"
/>
<SOURCE
TYPE="RESTING"
MANUFACTURER="Mortara Instrument, Inc."
MANUFACTURER_ID="10"
MODEL="el250"
ID="4"
TRANSMISSION_TIME="20070414092347"
/>
<AUTOMATIC_INTERPRETATION>
<STATEMENT
STATEMENT_NUMBER="1"
TEXT="SINUS RHYTHM WITH SHORT PR INTERVAL"
REASON=""
/>
<STATEMENT
STATEMENT_NUMBER="2"
TEXT="TYPE A WOLFF-PARKINSON-WHITE PATTERN"
REASON=""
/>
<STATEMENT
STATEMENT_NUMBER="3"
TEXT="NO FURTHER INTERPRETATION POSSIBLE"
REASON=""
/>
<STATEMENT
STATEMENT_NUMBER="4"
TEXT="ATYPICAL ECG"
REASON=""
/>
<STATEMENT
STATEMENT_NUMBER="5"
TEXT=""
REASON=""
/>
<STATEMENT
STATEMENT_NUMBER="6"
TEXT="UNCONFIRMED REPORT"
REASON=""
/>
</AUTOMATIC_INTERPRETATION>
<TYPICAL_CYCLE
R_PEAK="500"
P_ONSET="-185"
P_OFFSET="-116"
Q_ONSET="-87"
Q_OFFSET="75"
T_OFFSET="369"
P_DURATION="69"
PR_DURATION="98"
QRS_DURATION="162"
QT="456"
QTC="465"
QTCB="471"
QTCF="466"
P_AXIS="17"
QRS_AXIS="-54"
T_AXIS="108"
BITS="16"
FORMAT="SIGNED"
UNITS_PER_MV="400"
DURATION="1200"
SAMPLE_FREQ="1000"
ENCODING="BASE64"
>
<TYPICAL_CYCLE_CHANNEL
NAME="I"
DATA="BQAAAP7/AAAAAP7/AAACAAIAAgAAAP7//v/+/wAA/v8AAAIAAAAAAAIA/v/+
/wAAAAAAAAAAAAD+/wAAAAD7//v/AgACAAAA/v/+//v/+f/5//v/+f/7/wAA
AgACAP7/+//5//b/9P/0/wAA+/8AAAAA/v/7//T/8f/5//n//v/7//7/AgAA
AP7/AAD5//n//v/5//v/AAD7//n/AAAAAPv/+//7//7//v/5//v/+f/5//v/
+f/5//v/AAD7//n/+f/5//n/9v/2//T/+f/5//v/9v/+//n/+f/5//n/+f/+
//n/+f/5//n//v8AAPv/+f/7//n/+f/2//T//v/5//b/9v/0//v//v/5//b/
9v/0//T/9P/x//H/9P/7//7//v/2//n/+//5//n/9P/5//n/+//x//T/7//0
//b/8f/2//n/+//7/wAA9v/2//H/9P/0//T/7//x//n/9v/7/wAA+//5//b/
9v/2//b/9v/v//H/9v/0//n/+f/2//T/7//x//b/9v/0//b/+f/x//H/8f/0
//T/8f/5//n/9v/5//n/9v/2//b/+f/2//b/9v/+//n/9P/s//H/9P/2//H/
7//x//b/9v/0//T/9v/5//b/9P/2//T/+//+//H/7P/q/+//9P/v//H/9v/x
//H/9P/0//b/9v/5//T/+f/2//v/+//2//T/9P/2//b/9v/x//T/9v/2//H/
9P/5//b/8f/0//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g/+D/3f/g/+D/4P/l/+f/5f/g/+X/4v/g/9v/2//b/+L/5f/i/9v/2//d
/93/3f/b/9
答案 0 :(得分:0)
戴夫 让我重新发布代码和示例xml。我删除了错误捕获条件。
QString inputFilePath(QCoreApplication::arguments().at(1));
QFile inputFile(inputFilePath);
QString outputfilepath="/home/shakeel/xmlstreamlint/testing.xml";
QFile outputFile(outputfilepath);
QByteArray array[2000];
QByteArray *ptr;
ptr=&array[0];
QXmlStreamReader reader(&inputFile);
QXmlStreamWriter writer(ptr);
QString text,signal;
while (!reader.atEnd())
{
reader.readNext();
} else
if (reader.isStartElement())
{
text = reader.name().toString();
if(text.operator ==("TYPICAL_CYCLE_CHANNEL"))
{
signal=reader.readElementtext();
}
}
writer.writeCurrentToken(reader);
}
答案 1 :(得分:0)
只需添加结束括号xml即可完成 看看这是否有帮助 此致