I am trying to parse in Delphi xml like this:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>java.lang.ClassNotFoundException</faultstring>
</soap:Fault>
</soap:Body>
</soap:Envelope>
Delphi code:
program TestXML;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
Xml.xmldom,
Xml.XMLIntf,
Xml.XMLDoc,
ActiveX;
var
XMLDoc: TXMLDocument;
Root,Body,Fault,Node: IXMLNode;
begin
CoInitialize(nil);
try
XMLDoc := TXMLDocument.Create(nil);
XMLDoc.Options := XmlDoc.Options - [doNodeAutoCreate];
XMLDoc.LoadFromFile('test.xml');
Root := XMLDoc.DocumentElement;
Body := Root.ChildNodes['Body'];
Writeln(Body.NodeName);
Fault := Body.ChildNodes['Fault'];
Writeln(Fault.NodeName);
try
Node := Fault.ChildNodes['faultcode'];
Writeln(Node.Text);
except
on E:exception do Writeln(E.Message); // "Node "faultcode not found".
end;
finally
CoUnInitialize;
end;
end.
I guess the problem is: the parent node "Fault" has the namespace "http://schemas.xmlsoap.org/soap/envelope/" and has the prefix "soap" but the child node "faultcode" has no namespace and has no prefix.
How is it possibe to get an interface to the node "faultcode"?
Thank you.
This code works fine:
Fault.ChildNodes.FindNode('faultcode', '');
where empty string in the second parameter means no namespace.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With