Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Parse an xml



Permlink Replies: 2 - Last Post: Sep 3, 2016 9:21 AM Last Post By: Ole Ekerhovd
Ole Ekerhovd

Posts: 50
Registered: 2/20/03
Parse an xml
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 2, 2016 10:30 PM
I try to read a text propery from an xml file, but it's always emty.

I want to read the text value of <msg-no> In this case, 26006.

Here is the file:

<outbox-post-response version="1.0">
<message>
<self>https://ap-test.unit4.com/outbox/26006</self>
<xml-document>https://ap-test.unit4.com/messages/26006/xml-document</xml-document>
<message-meta-data>
<msg-no>26006</msg-no>
<direction>OUT</direction>
<received>2016-09-03T04:56:51.125+02:00</received>
<peppol-header>
<sender>9908:961329310</sender>
<receiver>9908:933355535</receiver>
<channel>EHF</channel>
<document-type>INVOICE</document-type>
<document-id>urn:oasis:names:specification:ubl:schema:xsd:Invoice-2::Invoice.......
<process-name>UNKNOWN</process-name>
<process-id>urn:www.cenbii.eu:profile:bii04:ver2.0</process-id>
</peppol-header>
</message-meta-data>
</message>

</outbox-post-response>

Here is my code:

procedure Tfrm.Button2Click(Sender: TObject);
var
aNumber: IXMLNode;
begin

XMLDocument1.Active := True;
try

// aNumber:=xmldocument1.DocumentElement.ChildNodes[0];
aNumber := XMLDocument1.DocumentElement;
showmessage(aNumber.ChildNodes['msg-no'].Text);

finally
XMLDocument1.Active := False;
end;

end;

What am I doing wrong?

Ole
Peter Below

Posts: 1,227
Registered: 12/16/99
Re: Parse an xml
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 3, 2016 1:27 AM   in response to: Ole Ekerhovd in response to: Ole Ekerhovd
Ole Ekerhovd wrote:

I try to read a text propery from an xml file, but it's always emty.

I want to read the text value of <msg-no> In this case, 26006.

Here is the file:

<outbox-post-response version="1.0">
<message>
<self>https://ap-test.unit4.com/outbox/26006</self>

<xml-document>https://ap-test.unit4.com/messages/26006/xml-document</x
ml-document> <message-meta-data>
<msg-no>26006</msg-no> <direction>OUT</direction>
<received>2016-09-03T04:56:51.125+02:00</received>
<peppol-header>
<sender>9908:961329310</sender>
<receiver>9908:933355535</receiver>
<channel>EHF</channel>
<document-type>INVOICE</document-type>

<document-id>urn:oasis:names:specification:ubl:schema:xsd:Invoice-2::I
nvoice....... <process-name>UNKNOWN</process-name>
<process-id>urn:www.cenbii.eu:profile:bii04:ver2.0</process-id>
</peppol-header> </message-meta-data>
</message>

</outbox-post-response>

Here is my code:

procedure Tfrm.Button2Click(Sender: TObject);
var
aNumber: IXMLNode;
begin

XMLDocument1.Active := True;
try

// aNumber:=xmldocument1.DocumentElement.ChildNodes[0];
aNumber := XMLDocument1.DocumentElement;
showmessage(aNumber.ChildNodes['msg-no'].Text);

finally
XMLDocument1.Active := False;
end;

end;

What am I doing wrong?

The node you are after is several levels down from the document node.
The ChildNodes collection only contains the immediate child nodes, not
also grand and grand-grand children.

You either drill down the node hierarchy manually, or use an xpath
expression to find the node you are after:

procedure TForm1.Button2Click(Sender: TObject);
var
LNode: IDomNode;
begin
xmldocument1.LoadFromFile('c:\temp\test.xml');
LNode:= (xmldocument1.DocumentElement.DOMNode as IDomNodeSelect).
selectNode('message/message-meta-data/msg-no');
if assigned(LNode) then
ShowMessage(LNode.nodeName + '= "'+
LNode.childNodes.item[0].nodeValue + '"')
else
ShowMessage('Node not found');
end;

If you work with IDOMNode the access to the node content is a bit
weird, since the text content is not the NodeValue of the found node
itself, but the NodeValue of the first child node, which has no node
name.

--
Peter Below
TeamB

Ole Ekerhovd

Posts: 50
Registered: 2/20/03
Re: Parse an xml
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 3, 2016 9:21 AM   in response to: Peter Below in response to: Peter Below

The node you are after is several levels down from the document node.
The ChildNodes collection only contains the immediate child nodes, not
also grand and grand-grand children.

You either drill down the node hierarchy manually, or use an xpath
expression to find the node you are after:

procedure TForm1.Button2Click(Sender: TObject);
var
LNode: IDomNode;
begin
xmldocument1.LoadFromFile('c:\temp\test.xml');
LNode:= (xmldocument1.DocumentElement.DOMNode as IDomNodeSelect).
selectNode('message/message-meta-data/msg-no');
if assigned(LNode) then
ShowMessage(LNode.nodeName + '= "'+
LNode.childNodes.item[0].nodeValue + '"')
else
ShowMessage('Node not found');
end;

If you work with IDOMNode the access to the node content is a bit
weird, since the text content is not the NodeValue of the found node
itself, but the NodeValue of the first child node, which has no node
name.

--
Peter Below
TeamB

Thanks Peter

Works great :)

Ole
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02