2013-07-02 26 views
0

我的XML程序:-----QT - xml文件 - 而不是對矯正文本節點內容

int MainWindow::xmlOpenFile() 
{ 
    //set the name of the file 
    xmlFile.setFileName(xmlFileName); 

    // open read & write mode 
    if (!xmlFile.open(QIODevice::ReadWrite|QIODevice::Text)) 
    { 
     return FAIL_TO_OPEN_FILE; 
    } 

    // Assign file to the stream 
    xmlStream = new QTextStream(&xmlFile); 

    xmlDomDocument.setContent(&xmlFile); 

    return SUCCESS_TO_OPEN_FILE; 
} 

void MainWindow::xmlAddRoot() 
{ 
    // Make the root element 
    xmlRoot = xmlDomDocument.createElement(ROOT_ELEMENT_DETAILS); 

    // Add it to the document 
    xmlDomDocument.appendChild(xmlRoot); 
} 

void MainWindow::xmlCreateNode(QDomElement &NodeElement, QString Name) 
{ 
    // Make the node element 
    NodeElement = xmlDomDocument.createElement(Name); 

} 

void MainWindow::xmlAddTextNode(QDomElement &NodeElement, QString textContent) 
{ 
    QDomText textNode; 
    textNode = xmlDomDocument.createTextNode(textContent); 
    NodeElement.appendChild(textNode); 
} 

void MainWindow::xmlAppendNode(QDomElement &xmlParent, QDomElement &xmlChild) 
{ 
    xmlParent.appendChild(xmlChild); 

} 

這是我的計劃中empty XML文件,輸入文字,MyXML.xml:----

xmlFileName = "D:/Temp/MyXML.xml"; 

xmlOpenFile(); 
xmlAddRoot(); 

xmlCreateNode(xmlTempNode, "hello"); 
xmlAddTextNode(xmlTempNode, "hi"); 
xmlAppendNode(xmlRoot,xmlTempNode); 

xmlCreateNode(xmlTempNode, "hello 0"); 
xmlAddTextNode(xmlTempNode, "hi 0"); 
xmlAppendNode(xmlRoot,xmlTempNode); 

xmlCreateNode(xmlTempNode, "hello -1"); 
xmlCreateNode(xmlTempNodeChild, "hello -1 - 1"); 
xmlAddTextNode(xmlTempNodeChild, "hi -1 - 1"); 
xmlAppendNode(xmlTempNode,xmlTempNodeChild); 
xmlCreateNode(xmlTempNodeChild, "hello -1 - 2"); 
xmlAddTextNode(xmlTempNodeChild, "hi -1 - 2"); 
xmlAppendNode(xmlTempNode,xmlTempNodeChild); 
xmlAppendNode(xmlRoot,xmlTempNode); 

xmlCreateNode(xmlTempNode, "hello -2"); 
xmlAddTextNode(xmlTempNode, "hi -2"); 
xmlAppendNode(xmlRoot,xmlTempNode); 

// Here i am replacingthe node, hello -1 
QDomNode temp_1 = xmlRoot.firstChild().nextSibling().nextSibling(); 
QDomNode temp = temp_1; 
QDomElement element = temp.toElement(); 
QDomNode n = element.firstChild(); 
QDomText t = n.toText(); 
t.setData("Here is the new text"); 

//Replace node "hello -1" 
xmlRoot.replaceChild(temp,temp_1); 

xmlCloseFile(); 

在這裏,在上面的代碼中我做了4個節點,然後to root測試情況下,我修改了第3子xmlRoot.firstChild().nextSibling().nextSibling();,但改變也不會在最後的XML文件中反映

它創造了後續荷蘭國際集團的xml文件:----

<Test Cases> 
<hello>hi</hello> 
<hello 0>hi 0</hello 0> 
<hello -1> 
    <hello -1 - 1>hi -1 - 1</hello -1 - 1> 
    <hello -1 - 2>hi -1 - 2</hello -1 - 2> 
</hello -1> 
<hello -2>hi -2</hello -2> 
</Test Cases> 

,但我想,以取代"hello -1"節點:---

<hello -1> 
    <hello -1 - 1>Here is the new text</hello -1 - 1> 
    <hello -1 - 2>hi -1 - 2</hello -1 - 2> 
</hello -1> 

爲什麼更換的孩子沒有工作?

回答

0

這裏是解決方案: ---

QDomNode temp_1 = xmlRoot.firstChild().nextSibling().nextSibling(); 
QDomElement temp; 

xmlCreateNode(temp,"hello -1"); 
xmlCreateNode(xmlTempNodeChild, "hello -1 - 1"); 
xmlAddTextNode(xmlTempNodeChild, "Here is the new text"); 
xmlAppendNode(temp,xmlTempNodeChild); 
xmlRoot.replaceChild(temp,temp_1); 
相關問題