void XmlText::readXml()
{
QFile file(m_strXmlPath);
if(!file.open(QFile::ReadWrite))
{
file.close();
return;
}
QDomDocument doc;
doc.setContent(file.readAll());
//根节点
QDomElement root = doc.documentElement();
QDomElement routeElemnet = root.firstChildElement("route");
QString routename = routeElemnet.attribute("routeName");
qDebug()<<routename;
QDomNodeList nodelist = routeElemnet.childNodes();
for(int i = 0; i < nodelist.count();++i)
{
QDomElement pointElement = nodelist.at(i).toElement();
int point = pointElement.attribute("point").toInt();
double lon = pointElement.attribute("longitde").toDouble();
double lat = pointElement.attribute("latitude").toDouble();
double speed = pointElement.attribute("speed").toDouble();
qDebug()<<point<<lon<<lat<<speed;
}
QDomElement entityElemnet = root.firstChildElement("entity");
QString name = entityElemnet.attribute("name");
QString id = entityElemnet.attribute("id");
QString type = entityElemnet.attribute("type");
qDebug()<<name<<id<<type;
file.close();
}
void XmlText::createWriteXml()
{
QDomDocument doc;
QDomProcessingInstruction instruction = doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
doc.appendChild(instruction);
QDomElement root = doc.createElement("root");
doc.appendChild(root);
QDomElement routeElemnt = doc.createElement("route");
root.appendChild(routeElemnt);
routeElemnt.setAttribute("routeName","航线1");
for(int i = 0; i < 5; ++i)
{
QDomElement pointElement = doc.createElement("routePoint");
routeElemnt.appendChild(pointElement);
pointElement.setAttribute("point",QString::number(i+1));
pointElement.setAttribute("lon",QString::number(i+18.11111));
pointElement.setAttribute("lat",QString::number(i+20.22222));
pointElement.setAttribute("speed",QString::number(i+5));
}
QDomElement entityElemnt = doc.createElement("entity");
root.appendChild(entityElemnt);
entityElemnt.setAttribute("name","舰船");
entityElemnt.setAttribute("id","123");
entityElemnt.setAttribute("type","海");
QFile file("D:/XjQtWork/config/123.xml");
if(file.open(QIODevice::ReadWrite | QIODevice::Text))
{
QTextStream stream(&file);
doc.save(stream, QDomNode::EncodingFromDocument);
file.close();
}
}
void XmlText::modifyXml()
{
QFile file("D:/XjQtWork/config/123.xml");
if(!file.open(QFile::ReadOnly | QFile::Text))
{
file.close();
return;
}
QDomDocument doc;
if(!doc.setContent(&file))
{
file.close();
return;
}
file.close();
QDomElement root = doc.documentElement();
QDomNodeList nodeList = root.childNodes();
QDomElement oldelemnet = root.firstChildElement("entity");
QDomElement newElemnet = doc.createElement("entity");
newElemnet.setAttribute("name","舰船1");
newElemnet.setAttribute("id","111");
newElemnet.setAttribute("type","海1");
newElemnet.setAttribute("dan","no");
root.replaceChild(newElemnet,oldelemnet);
//先读进来,再重写,如果不用truncate就是在后面追加内容,达不到效果
if(!file.open(QFile::WriteOnly | QFile::Truncate))
return;
//输出到文件
QTextStream stream(&file);
doc.save(stream,4); //缩进4格
file.close();
}
void XmlText::removeXmlNode()
{
QFile file("D:/XjQtWork/config/123.xml");
if(!file.open(QFile::ReadOnly | QFile::Text))
{
file.close();
return;
}
QDomDocument doc;
if(!doc.setContent(&file))
{
file.close();
return;
}
file.close();
QDomElement root = doc.documentElement();
QDomNodeList nodeList = root.childNodes();
//删除所有节点
for(int i = 0; i < nodeList.count();++i)
{
QDomElement element = nodeList.at(i).toElement();
root.removeChild(element);
}
//删除某个指定节点
QDomElement elemnet = root.firstChildElement("entity");
root.removeChild(elemnet);
QDomElement newElemnet = doc.createElement("entity");
newElemnet.setAttribute("name","舰船1");
newElemnet.setAttribute("id","111");
newElemnet.setAttribute("type","海1");
newElemnet.setAttribute("dan","no");
root.replaceChild(newElemnet,elemnet);
//先读进来,再重写,如果不用truncate就是在后面追加内容,达不到效果
if(!file.open(QFile::WriteOnly | QFile::Truncate))
return;
//输出到文件
QTextStream stream(&file);
doc.save(stream,4); //缩进4格
file.close();
}
<?xml version='1.0' encoding='UTF-8'?>
<root>
<route routeName="航线1">
<routePoint speed="5" point="1" lat="20.2222" lon="18.1111"/>
<routePoint speed="6" point="2" lat="21.2222" lon="19.1111"/>
<routePoint speed="7" point="3" lat="22.2222" lon="20.1111"/>
<routePoint speed="8" point="4" lat="23.2222" lon="21.1111"/>
<routePoint speed="9" point="5" lat="24.2222" lon="22.1111"/>
</route>
<entity id="111" dan="no" type="海1" name="舰船1"/>
</root>