-2

I have the following XML.

<ArrayOfRapJ xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/pluriel.Models">
  <Rapj>
    <Libdep>% Fréquentation:</Libdep>
    <Total>36.860068259385665529010238910</Total>
  </Rapj>

I am using the following Delphi code to read the <Libdep> and <Total> values:

var
  DOC: IXMLDocument;
  i: Integer;
  OrderChilds, E1EDP01_Node: IXMLNode;
begin
  DOC := LoadXMLDocument('d:\Rapjrnprests.xml');

  for i := 0 to DOC.ChildNodes.Nodes['ArrayOfRapJ'].ChildNodes.Count - 1 do
  begin
    OrderChilds := DOC.ChildNodes.Nodes['RapJ'].ChildNodes[i];
    if OrderChilds.NodeName = 'RapJ' then  
    begin
      E1EDP01_Node := OrderChilds.ChildNodes.Nodes['Libdep'];
      if Assigned(E1EDP01_Node) then
        Memo1.Lines.Add(E1EDP01_Node.ChildNodes.Nodes['Total'].NodeValue)
    end;
  end;

The XML is using URL namespaces. How do I read the values?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
David
  • 11
  • 8
  • It's not massively clear what you're asking. Can you post any code you've got? – GHC Mar 04 '15 at 13:46
  • Hello. For future reference, you can always edit your own post and add any additional information to it. Posting answers which aren't answers are frowned upon. For the avoidance of doubt, I know nothing about Delphi - if your question is good (has all the right info in all the right places), you're more likely to get an answer from someone who knows about this type of stuff. – GHC Mar 04 '15 at 15:46
  • 1
    There's not a single question mark in this question... What is your question? – René Hoffmann Mar 04 '15 at 16:21

2 Answers2

0

Your code is not correct for the XML you have shown, even without taking the namespaces into account.

Try something more like this instead:

var
  Doc: IXMLDocument;
  i: Integer;
  Arr, Node: IXMLNode;
  Libdep, Total: string;
begin
  Memo1.Clear;
  Doc := LoadXMLDocument('d:\Rapjrnprests.xml');

  // assuming <RapJArray> is the top-most element of the XML...
  Arr := Doc.DocumentElement;

  for i := 0 to Arr.ChildNodes.Count - 1 do
  begin
    Node := Arr.ChildNodes.Nodes[i];
    if (Node.NodeName = 'RapJ') {and (Node.NamespaceURI = 'http://schemas.datacontract.org/2004/07/pluriel.Models')} then
    begin
      Libdep := VarToStr(Node.ChildValues['Libdep']);
      Total := VarToStr(Node.ChildValues['Total']);
      if (Libdep <> '') and (Total <> '') then
        Memo1.Lines.Add(Libdep + ' ' + Total);
    end;
  end;
end;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

To start with, you have a simple typo. XML is case sensitive; your XML contains (note the lowercase j), while your code contains RapJ (note the uppercase J). The code has to match the XML exactly.

You've also got some mishandling in your code. Here's a (much simplified) working example (tested in XE7).

// Used in order to avoid creating a disk file. XML unchanged from
// question except for adding the closing </ArrayOfRapJ> element,
// which was missing

const
  XMLText = '<ArrayOfRapJ xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/pluriel.Models">' +
            '  <Rapj>' +
            '    <Libdep>% Fréquentation:</Libdep>' +
            '    <Total>36.860068259385665529010238910</Total>' +
            '  </Rapj>' +
            '</ArrayOfRapJ>';

procedure TForm5.Button1Click(Sender: TObject);
var
  Doc: IXMLDocument;
  DocNode: IXMLNode;
  Node: IXMLNode;
  i: Integer;
begin
  Doc := LoadXMLData(XMLText);
  DocNode := Doc.ChildNodes[0];

  for i := 0 to DocNode.ChildNodes.Count - 1 do
  begin
    Node :=  DocNode.ChildNodes[i];
    if Node.NodeName = 'Rapj' then
    begin
      Memo1.Lines.Add(Node.ChildNodes['Libdep'].Text);
      Memo1.Lines.Add(Node.ChildNodes['Total'].Text);
    end
    else
      Memo1.Lines.Add('Skipped node ' + Node.NodeName);
  end;
end;
Ken White
  • 123,280
  • 14
  • 225
  • 444