Einzelnen Beitrag anzeigen

Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#1

Verschachtelte XML Datei schreiben

  Alt 15. Dez 2006, 10:28
Ich habe folgende XML-Datei
XML-Code:
 <?xml version="1.0" encoding="ISO-8859-1"?>
<root>
  <layers>
    <layer>
      <posx>44</posx>
      <posy>96</posy>
      <width>160</width>
      <height>128</height>
      <image>1.bmp</image>
    </layer>
    <layer>
      <posx>359</posx>
      <posy>94</posy>
      <width>160</width>
      <height>128</height>
      <image>2.bmp</image>
    </layer>
    <layer>
      <posx>202</posx>
      <posy>277</posy>
      <width>160</width>
      <height>227</height>
      <image>3.bmp</image>
    </layer>
  </layers>

  <textobjects>
    <textobject>
      <posx>232</posx>
      <posy>153</posy>
      <width>95</width>
      <height>22</height>
      <text>ewfkerfmerlmf</text>
    </textobject>
    <textobject>
      <posx>208</posx>
      <posy>527</posy>
      <width>162</width>
      <height>41</height>
      <text>svmdfklmvortjgotr058690356u045fkdnbgkl</text>
    </textobject>
  </textobjects>
</root>
Und die will ich jetzt auch schreiben.

Aber ich bekomme es einfach nicht hin, dass die layer Knoten im Knoten layers stehen:

Delphi-Quellcode:
procedure TForm2.btnSaveLayersClick(Sender: TObject);

  function createElement(const xmldoc: DomDocument; parentNode: IXMLDOMNode; szNodeName, szNodeValue: string): boolean;
  var
    node : IXMLDOMNode;
  begin
    Result := false;
    if (xmldoc = nil) or (szNodeName = '') then
      exit;

    node := xmldoc.createElement(szNodeName);
    Result := node <> nil;

    if (node <> nil) then
    begin
      if (szNodeValue <> '') then
        node.text := szNodeValue;
      if (parentNode <> nil) then
        parentNode.appendChild(node)
      else
        xmldoc.documentElement.appendChild(node);
    end;
  end;

var
  XMLFile : string;
  XMLDoc : DomDocument;
  XMLLayers : IXMLDOMNode;
  XMLNode : IXMLDOMNode;
  i : Integer;
begin
  XMLDoc := CoDOMDocument.Create;

  XMLDoc.loadXML('<?xml version="1.0" encoding="ISO-8859-1"?><root/>');

  XMLLayers := XMLDoc.createElement('layers');
  XMLDoc.documentElement.appendChild(XMLLayers);

  with ImageEnVect1 do
  begin
    for i := 1 to LayersCount - 1 do
    begin
      XMLNode := XMLDoc.createElement('layer');
      XMLDoc.documentElement.appendChild(XMLNode);
      createElement(XmlDoc, XMLNode, 'posx', IntToStr(Layers[i].PosX));
      XMLDoc.documentElement.appendChild(XMLNode);
    end;
  end;

  XMLFile := ExtractFilePath(ParamStr(0)) + ' Text.xml';
  XMLDoc.save(XMLFile);
end;
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat