我使用以下代码来创建XML文档:
procedure TForm1.btnCreateXMLClick(Sender: TObject);
var
rootName:string;
childName:string;
attrChild:string;
iXml: IDOMDocument;
iRoot, iNode, iNode2, iChild, iAttribute: IDOMNode;
begin
XMLDoc.Active:=false;
XMLDoc.XML.Text:='';
XMLDoc.Active:=true;
XMLDoc.FileName:='C:\Documents and Settings\a\Desktop\New Text Document.xml';
iXml := XmlDoc.DOMDocument;
//iRoot:=iXml.documentElement(iXml.createElement('xml'));
iRoot := iXml.appendChild(iXml.createElement ('xml'));
// node "test"
iNode := iRoot.appendChild (iXml.createElement ('test'));
iNode.appendChild (iXml.createElement ('test2'));
iChild := iNode.appendChild (iXml.createElement ('test3'));
iChild.appendChild (iXml.createTextNode('simple value'));
iNode.insertBefore (iXml.createElement ('test4'), iChild);
// node replication
iNode2 := iNode.cloneNode (True);
iRoot.appendChild (iNode2);
// add an attribute
iAttribute := iXml.createAttribute ('color');
iAttribute.nodeValue := 'red';
iNode2.attributes.setNamedItem (iAttribute);
// show XML in memo
memXMLOutput.Lines.Text:=FormatXMLData(XMLDoc.XML.Text);
end;
Run Code Online (Sandbox Code Playgroud)
我在memXMLOutput中获得输出,但是当在Notepad或IE中看到时,XML文档没有显示输出.问题出在哪儿?提前致谢
删除这个:
XMLDoc.FileName:='C:\Documents and Settings\a\Desktop\New Text Document.xml';
Run Code Online (Sandbox Code Playgroud)
在完成代码创建XML文档后添加类似的内容:
XMLDoc.SaveToFile('C:\Documents and Settings\a\Desktop\New Text Document.xml');
Run Code Online (Sandbox Code Playgroud)