Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   XML (https://www.delphipraxis.net/46-xml/)
-   -   Delphi xml - soap - AddChild ohne soap: (https://www.delphipraxis.net/193308-xml-soap-addchild-ohne-soap.html)

user0815 14. Jul 2017 11:01

xml - soap - AddChild ohne soap:
 
Hmmm, die Frage liest sich wahrscheinlich komisch. Ich erzeuge XML mit nachfolgendem Code.
Bei den Untereinträgen z.B. DataNode := HeaderItem.AddChild('User'); wird aber auch jeweils :soap davor ausgegeben.
Eigentlich soll die Datei wie manuell erzeugt (sl) aussehen.

Delphi-Quellcode:
uses
  ActiveX, Xml.XMLIntf, Xml.XMLDoc;

...

procedure TForm1.Button1Click(Sender: TObject);
var
  Document: IXMLDocument;
  Envelope: IXMLNode;
  Body: IXMLNode;
  HeaderItem: IXMLNode;
  DataNode: IXMLNode;
  MessageState: IXMLNode;

  sl: TStringList;
begin
  CoInitialize(nil);

  Document := NewXMLDocument;
  Document.Encoding := 'utf-8';
  Document.Options := [doNodeAutoIndent];

  Envelope := Document.AddChild('soap:Envelope');
  Envelope.Attributes['xmlns:xsi'] := 'http://www.w3.org/2001/XMLSchema-instance';
  Envelope.Attributes['xmlns:xsd'] := 'http://www.w3.org/2001/XMLSchema';
  Envelope.Attributes['xmlns:soap'] := 'http://schemas.xmlsoap.org/soap/envelope/';

  Body := Envelope.AddChild('soap:Header');
  HeaderItem := Body.AddChild('HeaderItem');
  HeaderItem.Attributes['xmlns'] := 'http://tempuri.org/';

  DataNode := HeaderItem.AddChild('User');
  DataNode.NodeValue := 'Username';
  DataNode := HeaderItem.AddChild('Code');
  DataNode := HeaderItem.AddChild('Message');
  DataNode := HeaderItem.AddChild('Gender');

  Body := Envelope.AddChild('soap:Body');

  MessageState := Body.AddChild('MessageState');
  MessageState.Attributes['xmlns'] := 'http://tempuri.org/';
  DataNode := MessageState.AddChild('NewState');
  DataNode.NodeValue := '123456789';

  Memo1.Lines.Append(Document.Xml.Text);
  Memo1.Lines.Append('####################################################');

  Document.Xml.Clear;

  sl := TStringList.Create;
  try
    sl.Add('<?xml version="1.0"?>');
    sl.Add('<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">');
    sl.Add(' <soap:Header>');
    sl.Add('   <HeaderItem xmlns="http://tempuri.org/">');
    sl.Add('     <User>Username</User>');
    sl.Add('     <Code></Code/>');
    sl.Add('     <Message></Message>');
    sl.Add('     <Gender></Gender>');
    sl.Add('   </HeaderItem>');
    sl.Add(' </soap:Header>');
    sl.Add(' <soap:Body>');
    sl.Add('   <MessageState xmlns="http://tempuri.org/">');
    sl.Add('     <NewState>123456789</NewState>');
    sl.Add('   </MessageState>');
    sl.Add(' </soap:Body>');
    sl.Add('</soap:Envelope>');

    Document.Xml.AddStrings(sl);
  finally
    sl.Free;
  end;

  Memo1.Lines.Append(Document.Xml.Text);
  CoUninitialize;
end;
Zitat:

erste Ausgabe: <soap:User>Username</soap:User>
soll aber wie in der zweiten aussehen: <User>Username</User>
Was mache ich falsch, wieso wird ein soap: davor eingefügt ?

mjustin 14. Jul 2017 15:19

AW: xml - soap - AddChild ohne soap:
 
Der Fehler liegt wahrscheinlich hier:

Delphi-Quellcode:
  HeaderItem := Body.AddChild('HeaderItem');
  HeaderItem.Attributes['xmlns'] := 'http://tempuri.org/';
Es genügt nicht ein xmlns Attribut manuell hinzuzufügen, da dadurch das DOM nicht merkt dass es ein Namespace ist.

Namespace Undeclaration ist das Stichwort, eventuell hilft dies weiter:

https://stackoverflow.com/questions/...te-from-parent

Zitat:

You have to declare a namespace before you can use it. It is not enough to simply add an xmlns attribute manually, which is not the correct way to let the DOM know about the existence of the namespace. Use IXMLNode.DeclareNamespace() for that instead, eg:
Delphi-Quellcode:
CurNode := RootNode.AddChild('Child1');
CurNode.DeclareNamespace('', 'apenootje');

user0815 17. Jul 2017 07:29

AW: xml - soap - AddChild ohne soap:
 
Danke, das hat mich auf den richtigen Weg gebracht.
Bei den folgenden Zeilen muss man nur ", EmptyStr" anfügen, dann funktioniert es wie gewünscht.

Delphi-Quellcode:
...
HeaderItem := Body.AddChild('HeaderItem', EmptyStr);
...
MessageState := Body.AddChild('MessageState', EmptyStr);
...


Alle Zeitangaben in WEZ +1. Es ist jetzt 15:51 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