<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How can deal with such xml files?]]></title><description><![CDATA[<p dir="auto">Wish you can help me with two problems!<br />
1.How can I read the size information in such xml files ?<br />
2.How can I generate such xml files?</p>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/6cff6033-c531-4061-9ec6-1e0a437f1d60.jpg" alt="0_1500775170959_1.jpg" class=" img-fluid img-markdown" /></p>
]]></description><link>https://forum.qt.io/topic/81604/how-can-deal-with-such-xml-files</link><generator>RSS for Node</generator><lastBuildDate>Thu, 18 Jun 2026 07:13:01 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/81604.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 23 Jul 2017 01:59:38 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How can deal with such xml files? on Mon, 24 Jul 2017 01:51:59 GMT]]></title><description><![CDATA[<p dir="auto">Thank you very much!</p>
]]></description><link>https://forum.qt.io/post/406238</link><guid isPermaLink="true">https://forum.qt.io/post/406238</guid><dc:creator><![CDATA[Geng.Y]]></dc:creator><pubDate>Mon, 24 Jul 2017 01:51:59 GMT</pubDate></item><item><title><![CDATA[Reply to How can deal with such xml files? on Mon, 24 Jul 2017 00:42:10 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/geng.y">@<bdi>Geng.Y</bdi></a></p>
<p dir="auto">ok,let's to see the function <code>readNext</code>.</p>
<blockquote>
<p dir="auto">TokenType QXmlStreamReader::readNext()<br />
Reads the next token and returns its type.</p>
</blockquote>
<h4>1. what is the <strong>next token</strong> ?</h4>
<p dir="auto">you can print the token to string.</p>
<blockquote>
<p dir="auto">QString QXmlStreamReader::tokenString() const<br />
Returns the reader's current token as string.</p>
</blockquote>
<h4>2. what is the <strong>type</strong>?</h4>
<blockquote>
<p dir="auto">QXmlStreamReader::NoToken<br />
QXmlStreamReader::Invalid<br />
QXmlStreamReader::StartDocument<br />
QXmlStreamReader::EndDocument<br />
QXmlStreamReader::StartElement<br />
QXmlStreamReader::EndElement<br />
QXmlStreamReader::Characters<br />
QXmlStreamReader::Comment<br />
QXmlStreamReader::DTD<br />
QXmlStreamReader::EntityReference<br />
QXmlStreamReader::ProcessingInstruction</p>
</blockquote>
<p dir="auto"><strong>you should to check every type whether is you want, after you call <code>readNext</code>.</strong></p>
<p dir="auto">from the code, <code>readNext</code> return <code>Characters</code> token type.</p>
<blockquote>
<p dir="auto">QXmlStreamReader::Characters The reader reports characters in text(). If the characters are all white-space, isWhitespace() returns true. If the characters stem from a CDATA section, isCDATA() returns true.</p>
</blockquote>
<h4>3. How to understanding <strong>xml tokens</strong>?</h4>
<p dir="auto">go to <a href="http://xmlbeans.apache.org/docs/2.0.0/guide/conUnderstandingXMLTokens.html" target="_blank" rel="noopener noreferrer nofollow ugc">here</a></p>
]]></description><link>https://forum.qt.io/post/406235</link><guid isPermaLink="true">https://forum.qt.io/post/406235</guid><dc:creator><![CDATA[joeQ]]></dc:creator><pubDate>Mon, 24 Jul 2017 00:42:10 GMT</pubDate></item><item><title><![CDATA[Reply to How can deal with such xml files? on Sun, 23 Jul 2017 12:17:32 GMT]]></title><description><![CDATA[<p dir="auto">Thank you very much, I'm still puzzled about the "reader.readNext()"<img src="https://ddgobkiprc33d.cloudfront.net/5cb66036-5bd3-455b-9c82-6d2508ea70ba.jpg" alt="0_1500812056968_QQ截图20170723200923.jpg" class=" img-fluid img-markdown" /></p>
<p dir="auto">And I can't find the output of qDebug. It should be in the output window, right? But my output window is constantly emitting such kind of informations.<img src="https://ddgobkiprc33d.cloudfront.net/7718df22-0f6f-4de1-870f-d6ad791c44b5.jpg" alt="0_1500812242091_QQ截图20170723201610.jpg" class=" img-fluid img-markdown" /></p>
]]></description><link>https://forum.qt.io/post/406178</link><guid isPermaLink="true">https://forum.qt.io/post/406178</guid><dc:creator><![CDATA[Geng.Y]]></dc:creator><pubDate>Sun, 23 Jul 2017 12:17:32 GMT</pubDate></item><item><title><![CDATA[Reply to How can deal with such xml files? on Sun, 23 Jul 2017 11:27:18 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/geng.y">@<bdi>Geng.Y</bdi></a> Hi, friend.</p>
<p dir="auto">I write the code in my way. it maybe safer than your code.</p>
<pre><code>void ParseXml::ReadBndbox()
{
    if(reader.name() == "bndbox"){
        reader.readNext();
    }else{
       /// maybe return false at here.
    }

    while(!reader.isStartElement()){
        reader.readNext();
    }

    m_xmin = ReadItemPos("xmin");
    m_ymin = ReadItemPos("ymin");
    m_xmax = ReadItemPos("xmax");
    m_ymax = ReadItemPos("ymax");
    Q_ASSERT(m_xmin &gt;= 0 &amp;&amp; m_ymin &gt;= 0);
    Q_ASSERT(m_xmax &gt;= 0 &amp;&amp; m_ymax &gt;= 0);
    qDebug("%d %d %d %d",m_xmin,m_ymin,m_xmax,m_ymax);
}

int ParseXml::ReadItemPos(QString itemName)
{
    int nVal = -1;
    bool bOk;
    while(!reader.isStartElement()){
        reader.readNext();
    }

    if(reader.name() == itemName){
        nVal = reader.readElementText().toInt(&amp;bOk);
        Q_ASSERT(bOk == true);
        reader.readNext();
    }

    return nVal;
}

</code></pre>
]]></description><link>https://forum.qt.io/post/406176</link><guid isPermaLink="true">https://forum.qt.io/post/406176</guid><dc:creator><![CDATA[joeQ]]></dc:creator><pubDate>Sun, 23 Jul 2017 11:27:18 GMT</pubDate></item><item><title><![CDATA[Reply to How can deal with such xml files? on Sun, 23 Jul 2017 11:00:18 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/geng.y">@<bdi>Geng.Y</bdi></a></p>
<p dir="auto">Hi, I found your error. below the code, you will know where are you wrong.</p>
<pre><code>void ParseXml::ReadBndbox()
{
    bool bOk;
    int points[4];

    reader.readNext(); ///&lt; Note: here, skip the '\n' some characters
    for (int i=0;i&lt;4;i++)
    {
        bOk = false;
        reader.readNext();
        points[i] = reader.readElementText().toInt(&amp;bOk);
        if(!bOk){
            qDebug("toInt data failed");
        }
        reader.readNext();
    }

    m_xmin = points[0];
    m_ymin = points[1];
    m_xmax = points[2];
    m_ymax = points[3];
    qDebug("%d %d %d %d\n",m_xmin,m_ymin,m_xmax,m_ymax);
}
</code></pre>
<p dir="auto">If i modified the xml file format, the code has error.</p>
<pre><code>        &lt;bndbox&gt;&lt;xmin&gt;144&lt;/xmin&gt; ///&lt; not has '\n' ,will has error
            &lt;ymin&gt;485&lt;/ymin&gt;
            &lt;xmax&gt;164&lt;/xmax&gt;
            &lt;ymax&gt;515&lt;/ymax&gt;
        &lt;/bndbox&gt;
</code></pre>
<p dir="auto"><strong>QXmlStream* =&gt; io stream</strong></p>
]]></description><link>https://forum.qt.io/post/406174</link><guid isPermaLink="true">https://forum.qt.io/post/406174</guid><dc:creator><![CDATA[joeQ]]></dc:creator><pubDate>Sun, 23 Jul 2017 11:00:18 GMT</pubDate></item><item><title><![CDATA[Reply to How can deal with such xml files? on Sun, 23 Jul 2017 09:59:25 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/joeq">@<bdi>joeQ</bdi></a><br />
I‘ve tried to read four values from the 0.xml,xmin,ymin,xmax and ymax in "bndbox", and tp draw a rectangle with them.But it doesn't work. I've debuged only to find that I cannot get those values, though the reader can find "bndbox" well. There must have been something wrong in small details, could you please help me to figure out?</p>
<p dir="auto">The class ParseXml is used in ImageWidget.</p>
<pre><code>//parsexml.h

#pragma once
#include &lt;QXmlStreamReader&gt;
#include &lt;QMessageBox&gt;
#include &lt;QString&gt;
#include &lt;QFile&gt;
class ParseXml
{
public:
	ParseXml(void);
	ParseXml(QString fileName);
	~ParseXml(void);

	void ReadFile(const QString &amp;fileName);
	void ReadAnnotation();
	void ReadObject();
	void ReadBndbox();
	void Skip();

	QPoint get_start_point();
	QPoint get_end_point();

private:
	QXmlStreamReader reader;
	int m_xmin;
	int m_ymin;
	int m_xmax;
	int m_ymax;
};
</code></pre>
<pre><code>//parsexml.cpp

#include "ParseXml.h"


ParseXml::ParseXml(void)
{
}

ParseXml::ParseXml(QString fileName)
{
	ReadFile(fileName);
}

ParseXml::~ParseXml(void)
{
}

void ParseXml::ReadFile(const QString &amp;fileName)
{
	QFile file(fileName);

	if (file.open(QIODevice::ReadOnly|QIODevice::Text))
	{
		reader.setDevice(&amp;file);

		while (!reader.atEnd())
		{
			if (reader.isStartElement())
			{
				if (reader.name()=="annotation")
				{
					ReadAnnotation();
				}
				else
				{
					reader.raiseError(QObject::tr("Not a valid annotation file"));
				}
			}
			else
			{
				reader.readNext();
			}
		}

		file.close();
	}
}

void ParseXml::ReadAnnotation()
{
	reader.readNext();

	while(!reader.atEnd())
	{
		if (reader.isEndElement())
		{
			reader.readNext();
			break;
		}

		if (reader.isStartElement())
		{
			if (reader.name()=="object")
			{
				ReadObject();
			}
			else
			{
				Skip();
			}
		}
		else
		{
			reader.readNext();
		}
	}
}

void ParseXml::ReadObject()
{
	reader.readNext();

	while (!reader.atEnd())
	{
		if (reader.isEndElement())
		{
			reader.readNext();
			break;
		}

		if (reader.isStartElement())
		{
			if (reader.name()=="bndbox")
			{
				ReadBndbox();
			}
			else
			{
				Skip();
			}
		}
		else
		{
			reader.readNext();
		}
	}
}

void ParseXml::ReadBndbox()
{
	int points[4];

	for (int i=0;i&lt;4;i++)
	{
		reader.readNext();
		points[i] = reader.readElementText().toInt();
		reader.readNext();
	}

	m_xmin = points[0];
	m_ymin = points[1];
	m_xmax = points[2];
	m_ymax = points[3];
}

void ParseXml::Skip()
{
	reader.readNext();

	while(!reader.atEnd())
	{
		if (reader.isEndElement())
		{
			reader.readNext();
			break;
		}

		if (reader.isStartElement())
		{
			Skip();
		}
		else
		{
			reader.readNext();
		}
	}
}

QPoint ParseXml::get_start_point()
{
	return QPoint(m_xmin,m_ymin);
}

QPoint ParseXml::get_end_point()
{
	return QPoint(m_xmax,m_ymax);
}
</code></pre>
<pre><code>//imagewidget.h

#ifndef IMAGEWIDGET_H
#define IMAGEWIDGET_H

#include &lt;QWidget&gt;
#include &lt;QImage&gt;
#include &lt;vector&gt;
#include &lt;QPainter&gt;
#include &lt;QFileDialog&gt;
#include &lt;QMouseEvent&gt;
#include &lt;QMessageBox&gt;
#include &lt;QXmlStreamReader&gt;
#include &lt;QToolTip&gt;
#include "ui_imagewidget.h"
#include "Rect.h"
#include "ParseXml.h"

using namespace std;

class ImageWidget : public QWidget
{
	Q_OBJECT

public:
	ImageWidget(QWidget *parent = 0);
	~ImageWidget();

public slots:
	void Open();
	void Save();
	void SaveAnnotation();
	void ReadXml();
	void SaveXml();
	void Delete();
	void paintEvent(QPaintEvent *);
	void mousePressEvent(QMouseEvent *event);
	void mouseMoveEvent(QMouseEvent *event);
	void mouseReleaseEvent(QMouseEvent *event);

private:
	Ui::ImageWidget ui;

	
	QImage *ptr_image;
	QImage *ptr_save;
	QPoint m_Start;
	QPoint m_End;
	bool draw_status;
	bool load_status;
	vector&lt;Rect*&gt; rect_array;


};

#endif // IMAGEWIDGET_H

</code></pre>
<pre><code>//imagewidget.cpp

#include "imagewidget.h"

ImageWidget::ImageWidget(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);

	ptr_image = new QImage();
	ptr_save = new QImage();
	draw_status = false;
	load_status = false;
	setMouseTracking(true);
	
}

ImageWidget::~ImageWidget()
{
	for (int i=0;i&lt;rect_array.size();i++)
	{
		if (rect_array[i])
		{
			delete rect_array[i];
			rect_array[i] = NULL;
		}
	}
}

void ImageWidget::Open()
{
	QString fileName = QFileDialog::getOpenFileName(this,tr("Read Image"),".",tr("Image(*.bmp *.png *.jpg)"));

	if (!fileName.isEmpty())
	{
		ptr_image-&gt;load(fileName);
		load_status = true;
		 
	}

	update();
}

void ImageWidget::Save()
{
	QString fileName = QFileDialog::getSaveFileName(this,tr("Save Image"),".",tr("Image(*.bpm *.png *.jpg)"));

	if (!fileName.isEmpty())
	{
		ptr_save-&gt;save(fileName);
		
	}
}

void ImageWidget::SaveAnnotation()
{

}

void ImageWidget::ReadXml()
{
	QString fileName = QFileDialog::getOpenFileName(this,tr("Read Xml File"),".",tr("Xml(*.xml)"));
	ParseXml parsexml_(fileName);
	m_Start = parsexml_.get_start_point();
	m_End = parsexml_.get_end_point();
}

void ImageWidget::SaveXml()
{
	QString fileName = QFileDialog::getSaveFileName(this,tr("Save Xml File"),".",tr("Xml(*.xml)"));
}

void ImageWidget::Delete()
{
	if (!rect_array.empty())
	{
		rect_array.pop_back();
	}
}

void ImageWidget::mousePressEvent(QMouseEvent *event)
{
	if (Qt::LeftButton==event-&gt;button()&amp;&amp;load_status)
	{
		draw_status = true;
		m_Start = m_End = event-&gt;pos();
	}
}

void ImageWidget::mouseMoveEvent(QMouseEvent *event)
{
	QToolTip::showText(event-&gt;globalPos(),QString::number( event-&gt;pos().x() ) + ", " +QString::number( event-&gt;pos().y() ),this, rect() );
	QWidget::mouseMoveEvent(event);
	
	if (draw_status)
	{
		m_End = event-&gt;pos();
	}
}

void ImageWidget::mouseReleaseEvent(QMouseEvent *event)
{
	Rect *current_rect = NULL;
	current_rect = new Rect(m_Start,m_End);
	rect_array.push_back(current_rect);
	draw_status = false;
}

void ImageWidget::paintEvent(QPaintEvent *)
{
	*ptr_save = *ptr_image;
	QPen pen;
	QPainter painter(this);
	QPainter paintImage(ptr_save);
	QRect image_rect(0,0,ptr_image-&gt;width(),ptr_image-&gt;height());

	
	pen.setWidth(5);
	pen.setColor(Qt::red);
	painter.setPen(pen);
	paintImage.setPen(pen);

	painter.begin(this);
	paintImage.begin(ptr_save);
	painter.drawImage(image_rect,*ptr_image);

	painter.drawRect(m_Start.rx(),m_Start.ry(),m_End.rx()-m_Start.rx(),m_End.ry()-m_End.ry());

	for (int i=0;i&lt;rect_array.size();i++)
	{
		rect_array[i]-&gt;Draw(painter);
		rect_array[i]-&gt;Draw(paintImage);
	}
	
	if (draw_status)
	{
		painter.drawRect(m_Start.rx(),m_Start.ry(),m_End.rx()-m_Start.rx(),m_End.ry()-m_Start.ry());
		paintImage.drawRect(m_Start.rx(),m_Start.ry(),m_End.rx()-m_Start.rx(),m_End.ry()-m_Start.ry());
	}

	painter.end();
	update();

}
</code></pre>
]]></description><link>https://forum.qt.io/post/406172</link><guid isPermaLink="true">https://forum.qt.io/post/406172</guid><dc:creator><![CDATA[Geng.Y]]></dc:creator><pubDate>Sun, 23 Jul 2017 09:59:25 GMT</pubDate></item><item><title><![CDATA[Reply to How can deal with such xml files? on Sun, 23 Jul 2017 03:54:10 GMT]]></title><description><![CDATA[<p dir="auto">I find it really complex to deal with xml files, but let me try!<br />
Thank you!</p>
]]></description><link>https://forum.qt.io/post/406160</link><guid isPermaLink="true">https://forum.qt.io/post/406160</guid><dc:creator><![CDATA[Geng.Y]]></dc:creator><pubDate>Sun, 23 Jul 2017 03:54:10 GMT</pubDate></item><item><title><![CDATA[Reply to How can deal with such xml files? on Sun, 23 Jul 2017 02:13:57 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/geng.y">@<bdi>Geng.Y</bdi></a><br />
Hi,friend,welcome.</p>
<ol>
<li>you can search key words of Qt <a href="http://doc.qt.io/qt-5/classes.html" target="_blank" rel="noopener noreferrer nofollow ugc">QXml</a>, you will get some Qt class about xml .</li>
<li>here are some demos about how to process xml file in qt. <a href="http://doc.qt.io/qt-5/qtexamples.html" target="_blank" rel="noopener noreferrer nofollow ugc">link</a></li>
</ol>
]]></description><link>https://forum.qt.io/post/406153</link><guid isPermaLink="true">https://forum.qt.io/post/406153</guid><dc:creator><![CDATA[joeQ]]></dc:creator><pubDate>Sun, 23 Jul 2017 02:13:57 GMT</pubDate></item></channel></rss>