Delphi xmlchildnode gets attribute from parent

I am trying to write xML in Delphi.

If the xmlns attribute has a node attribute, the node child of this node also shows the attribute, but then is empty. How can I preface that a child of a node shows an attribute?

I tested the following code

procedure TForm2.Button1Click(Sender: TObject);
var
  RootNode, CurNode, PmtNode, PmtDetNode : IXMLNODE;
  I:Integer;
begin
   SepaDoc := Newxmldocument;
   SepaDoc.Encoding := 'utf-8';
   SepaDoc.Options := [doNodeAutoIndent];
   RootNode := SepaDoc.AddChild('Document');
   CurNode := RootNode.AddChild('Child1');
   CurNode.Attributes['xmlns'] := 'apenootje';
   CurNode := CurNode.AddChild('Child2');
   CurNode := CurNode.AddChild('Child3');
   SepaDoc.SaveToFile('D:\indir\testsepa.xml');
end;

This result is shown in the following XML

<?xml version="1.0" encoding="UTF-8"?>
-<Document>   -<Child1 xmlns="apenootje">
    -<Child2 xmlns="">
       <Child3/>
     </Child2>
    </Child1>    
  </Document>

Thanks Rob Novey

+4
source share
2 answers

Since the children of Child1 do not carry the same namespace, it must be undeclared and that is the reason that Child2 contains an empty (default) namespace.

This is called a namespace symbol.

xmlns = ", " ": , .

XML Namespaces 1.1 . , xmlns: p =" " , p (, , ) ,

; :

program SO20424534;

{$APPTYPE CONSOLE}

uses
  ActiveX,
  XMLdom,
  XMLDoc,
  XMLIntf,
  SysUtils;

function TestXML : String;

var
  RootNode,
  CurNode    : IXMLNODE;
  Doc        : IXmlDocument;
  ns         : String;

begin
 Doc := Newxmldocument;
 ns := 'apenootje';
 Doc.Encoding := 'utf-8';
 Doc.Options := [doNodeAutoIndent];
 RootNode := Doc.AddChild('Document');
 CurNode := RootNode.AddChild('Child1');
 CurNode.DeclareNamespace('', ns);
 CurNode := CurNode.AddChild('Child2', ns);
 CurNode := CurNode.AddChild('Child3', ns);
 Result := Doc.XML.Text;
end;

begin
  try
   CoInitialize(nil);
   try 
    Writeln(TestXML);
   finally
    CoUninitialize;
   end;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
 Readln;
end;

:

<?xml version="1.0"?>
<Document>
  <Child1 xmlns="apenootje">
    <Child2>
      <Child3/>
    </Child2>
  </Child1>
</Document>
+5

, . xmlns , DOM . IXMLNode.DeclareNamespace(), :

CurNode := RootNode.AddChild('Child1');
CurNode.DeclareNamespace('', 'apenootje');
+2

All Articles