Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   XML (https://www.delphipraxis.net/46-xml/)
-   -   Delphi XML.Attributes entfernen (https://www.delphipraxis.net/95232-xml-attributes-entfernen.html)

Dunkel 3. Jul 2007 17:51


XML.Attributes entfernen
 
Hallo zusammen!

Seit kurzer Zeit beschäftige ich mich mit XML. Um genauer zu sein, versuche ich momentan das XSPF-Playlist-Format zu lesen & zu schreiben.

Das Lesen ist (nach einer seehhrr langen Nacht) nicht das Problem, das Schreiben im Prinzip auch nicht.

Hier ist der Test-Quelltext zum Schreiben eines XSPF-Dokuments:
Delphi-Quellcode:
var
  myXML: IXMLDocument;
  node: IXMLNode;
  i: word;
begin
  myXML := newXMLDocument;
  myXML.Options:= [doNodeAutoIndent];

  myXML.Encoding:= 'UTF-8';

  node:= myXML.AddChild('playlist');
  node.Attributes['version']:= '0';
  node.Attributes['xmlns']:= 'http://xspf.org/ns/0/';

  node:= myXML.DocumentElement.AddChild('trackList');

  for i := 1 to 5 do
    begin
      node:= myXML.DocumentElement.ChildNodes.FindNode('trackList').AddChild('track');
      node.AddChild('creator');
      node.ChildNodes.FindNode('creator').Text:= 'TestArtist'+IntToStr(i);
      node.AddChild('location');
      node.ChildNodes.FindNode('location').Text:= 'TestLocation'+IntToStr(i);
      node.AddChild('trackNum');
      node.ChildNodes.FindNode('trackNum').Text:= IntToStr(i);
    end;

  myXML.Active:= True;
  myXML.SaveToFile('D:\Test.xspf');
end;

Und so sieht die erzeugte Datei aus:
XML-Code:
<?xml version="1.0" encoding="UTF-8"?>
<playlist version="0" xmlns="http://xspf.org/ns/0/">
  <trackList xmlns="">
    <track>
      <creator>TestArtist1</creator>
      <location>TestLocation1</location>
      <trackNum>1</trackNum>
    </track>
    <track>
      <creator>TestArtist2</creator>
      <location>TestLocation2</location>
      <trackNum>2</trackNum>
    </track>
    <track>
      <creator>TestArtist3</creator>
      <location>TestLocation3</location>
      <trackNum>3</trackNum>
    </track>
    <track>
      <creator>TestArtist4</creator>
      <location>TestLocation4</location>
      <trackNum>4</trackNum>
    </track>
    <track>
      <creator>TestArtist5</creator>
      <location>TestLocation5</location>
      <trackNum>5</trackNum>
    </track>
  </trackList>
</playlist>
So weit, so gut. Das Problem ist nur, dass im Node trackList das Attribut xmlns gesetzt wird. Ich bin mir aber nicht bewusst, wo ich das in meinem Code mache.

Kann mir da jemand weiterhelfen?

marabu 3. Jul 2007 18:15

Re: XML.Attributes entfernen
 
Hallo,

dein Problem scheint mir hier schon einmal behandelt worden zu sein: Attribute die ich gar nicht explizit erstellt habe

Grüße vom marabu

Dunkel 3. Jul 2007 20:22

Re: XML.Attributes entfernen
 
Besten Dank für den Link, marabu!

Da ich aber nicht mit dem MS-DOM arbeite, hat mich das alles aber nicht wirklich weitergebracht. AppendChild & Co kennt IXMLDocument leider nicht; zumindest habe ich auf die Schnelle keine Entsprechende Funktion gefunden.

Mein Quick&Dirty-Workaround sieht momentan wie folgt aus:
Delphi-Quellcode:
  //myXML.SaveToFile('D:\Test.xspf');
  myXML.XML[2]:= StringReplace(myXML.XML[2], ' xmlns=""', '', [rfReplaceAll]);
  myXML.XML.SaveToFile('D:\Test.xspf');
Ist definitiv nicht schön, bringt mir aber das gewünschte Ergebnis. :stupid:

Ich werde aber noch weiter probieren, irgendwie muss es ja auch mit IXMLDocument gehen.

Deswegen lasse ich dieses Thema weiterhin offen; und hoffe auf eine Erleuchtung oder auf mehr Postings.

marabu 3. Jul 2007 20:47

Re: XML.Attributes entfernen
 
Hallo,

Zitat:

Zitat von Dunkel
... Da ich aber nicht mit dem MS-DOM arbeite, hat mich das alles aber nicht wirklich weitergebracht. ...

da du keine Angabe zum DOM-Vendor gemacht hattest, habe ich den Default unterstellt - und der ist MSXML.

Meine Erklärungen aus dem verlinkten Beitrag lassen sich übrigens so auf deine Umgebung übertragen:

Delphi-Quellcode:
const
  nsDefault = 'http://xspf.org/ns/0/';
var
  myXML: IXMLDocument;
  node: IXMLNode;
  i: word;
begin
  myXML := newXMLDocument;
  myXML.Options:= [doNodeAutoIndent];

  myXML.Encoding:= 'UTF-8';

  node:= myXML.AddChild('playlist');
  node.Attributes['version']:= '0';
  node.Attributes['xmlns']:= nsDefault;

  node:= myXML.DocumentElement.AddChild('trackList', nsDefault);

  // ...
Gute Nacht

Dunkel 3. Jul 2007 22:33

Re: XML.Attributes entfernen
 
marabu, you made my day! :-D
Funktioniert perfekt! DANKE! :cheers:


Alle Zeitangaben in WEZ +1. Es ist jetzt 10:34 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz