Einzelnen Beitrag anzeigen

hoika

Registriert seit: 5. Jul 2006
Ort: Magdeburg
8.270 Beiträge
 
Delphi 10.4 Sydney
 
#1

XE4: ADOM Core oder TXmlDocument, kann Datei nicht laden

  Alt 13. Feb 2015, 17:48
Hallo,

ich versuche, entweder per ADOM CORE oder TXmlDocument die angehängte XML-Datei zu laden.
eigentlich ist das doch eine simple Datei?

Mit der alten 2009-er ADOM unter D2007 klappt das ohne Problem.

Weiss jemand, warum das nicht geht?

Ich habe folgende Links benutzt:
http://docwiki.embarcadero.com/CodeE...e_%28Delphi%29

Der Code aus dem folgenden Link funktioniert,
dann muss ich aber meinen Quellcode kompletten umbauen:
http://www.experts-exchange.com/Prog..._28366764.html


Hier der durch die Testerei etwas wirre Code
Delphi-Quellcode:
unit XML_LesenTest;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm54 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form54: TForm54;

implementation

{$R *.dfm}

uses ComObj, XMLIntf, XMLDoc, MsXml;

(*
const
  CAttrName = 'attrName';
  HTAB = #9;
var
  LDocument: IXMLDocument;
  LNodeElement, LNode: IXMLNode;
  LAttrValue: string;
  I: Integer;
begin
  LDocument := TXMLDocument.Create(nil);
  LDocument.LoadFromFile(SrcPath); { File should exist. }

  { Find a specific node. }
  LNodeElement := LDocument.ChildNodes.FindNode('ThisIsTheDocumentElement');

  if (LNodeElement <> nil) then
  begin
    { Get a specific attribute. }
    Writeln('Getting attribute...');
    if (LNodeElement.HasAttribute(CAttrName)) then
    begin
      LAttrValue := LNodeElement.Attributes[CAttrName];
      Writeln('Attribute value: ' + LAttrValue);
    end;

    { Traverse child nodes. }
    Writeln(sLineBreak, 'Traversing child nodes...' + sLineBreak);
    for I := 0 to LNodeElement.ChildNodes.Count - 1 do
    begin
      LNode := LNodeElement.ChildNodes.Get(I);
      { Display node name. }
      Writeln(sLineBreak + 'Node name: ' + LNode.NodeName);
      { Check whether the node type is Text. }
      if LNode.NodeType = ntText then
      begin
        Writeln(HTAB + 'This is a node of type Text. The text is: ' + LNode.Text);
      end;
      { Check whether the node is text element. }
      if LNode.IsTextElement then
      begin
        Writeln(HTAB + 'This is a text element. The text is: ' + LNode.Text);
      end;
    end;
  end;
end;
*)


procedure CreateDocument;
var
  LDocument: IXMLDocument;
  LNodeElement, NodeCData, NodeText: IXMLNode;
begin
  LDocument := TXMLDocument.Create(nil);
  LDocument.Active := True;

  { Define document content. }
  LDocument.DocumentElement := LDocument.CreateNode('ThisIsTheDocumentElement', ntElement, '');
  LDocument.DocumentElement.Attributes['attrName'] := 'attrValue';
  LNodeElement := LDocument.DocumentElement.AddChild('ThisElementHasText', -1);
  LNodeElement.Text := 'Inner text.';
  NodeCData := LDocument.CreateNode('any characters here', ntCData, '');
  LDocument.DocumentElement.ChildNodes.Add(NodeCData);
  NodeText := LDocument.CreateNode('This is a text node.', ntText, '');
  LDocument.DocumentElement.ChildNodes.Add(NodeText);

  LDocument.SaveToFile('C:\ProjektA_TestDB\ICD10\1.xml');
end;


procedure TForm54.Button1Click(Sender: TObject);
begin
  CreateDocument;
end;
(*
const
  CAttrName = 'attrName';
  HTAB = #9;
var
  LDocument: IXMLDocument;
  LNodeElement, LNode: IXMLNode;
  LAttrValue: string;
  I: Integer;
  url: String;
begin
  // put url or file name
  url := 'C:\ProjektA_TestDB\ICD10\1.xml';
  //url := 'C:\ProjektA_TestDB\ICD10\2.xml';

  if not FileExists(url) then
  begin
    raise Exception.Create('XML file not found');
  end;

  LDocument := TXMLDocument.Create(nil);
  LDocument.LoadFromFile(url); { File should exist. }

  { Find a specific node. }
  LNodeElement := LDocument.ChildNodes.FindNode('kapitel_liste');

  if (LNodeElement <> nil) then
  begin
    { Get a specific attribute. }
    Writeln('Getting attribute...');
    if (LNodeElement.HasAttribute(CAttrName)) then
    begin
      LAttrValue := LNodeElement.Attributes[CAttrName];
      Writeln('Attribute value: ' + LAttrValue);
    end;

    { Traverse child nodes. }
    Writeln(sLineBreak, 'Traversing child nodes...' + sLineBreak);
    for I := 0 to LNodeElement.ChildNodes.Count - 1 do
    begin
      LNode := LNodeElement.ChildNodes.Get(I);
      { Display node name. }
      Writeln(sLineBreak + 'Node name: ' + LNode.NodeName);
      { Check whether the node type is Text. }
      if LNode.NodeType = ntText then
      begin
        Writeln(HTAB + 'This is a node of type Text. The text is: ' + LNode.Text);
      end;
      { Check whether the node is text element. }
      if LNode.IsTextElement then
      begin
        Writeln(HTAB + 'This is a text element. The text is: ' + LNode.Text);
      end;
    end;
  end;

end;
*)


procedure RetrieveDocument;
const
  CAttrName = 'attrName';
  HTAB = #9;
var
  LDocument: IXMLDocument;
  LNodeElement, LNode: IXMLNode;
  LAttrValue: string;
  I: Integer;
begin
  LDocument := TXMLDocument.Create(nil);
  LDocument.LoadFromFile('C:\ProjektA_TestDB\ICD10\1.xml'); { File should exist. }

  { Find a specific node. }
  LNodeElement := LDocument.ChildNodes.FindNode('BoolsAsChecks');

  if (LNodeElement <> nil) then
  begin
    { Get a specific attribute. }
    Writeln('Getting attribute...');
    if (LNodeElement.HasAttribute(CAttrName)) then
    begin
      LAttrValue := LNodeElement.Attributes[CAttrName];
      Writeln('Attribute value: ' + LAttrValue);
    end;

    { Traverse child nodes. }
    Writeln(sLineBreak, 'Traversing child nodes...' + sLineBreak);
    for I := 0 to LNodeElement.ChildNodes.Count - 1 do
    begin
      LNode := LNodeElement.ChildNodes.Get(I);
      { Display node name. }
      Writeln(sLineBreak + 'Node name: ' + LNode.NodeName);
      { Check whether the node type is Text. }
      if LNode.NodeType = ntText then
      begin
        Writeln(HTAB + 'This is a node of type Text. The text is: ' + LNode.Text);
      end;
      { Check whether the node is text element. }
      if LNode.IsTextElement then
      begin
        Writeln(HTAB + 'This is a text element. The text is: ' + LNode.Text);
      end;
    end;
  end;
end;

procedure TForm54.Button2Click(Sender: TObject);
begin
  RetrieveDocument;
end;

procedure TForm54.Button3Click(Sender: TObject);
var
  XMLDoc: IXMLDOMDocument;
begin
  XMLDoc := CoDOMDocument.Create;

  XMLDoc.load('C:\ProjektA_TestDB\ICD10\1.xml');
  Memo1.Lines.Add(XMLDoc.xml);
  Memo1.Lines.Add('');

  XMLDoc.load('C:\ProjektA_TestDB\ICD10\2.xml');
  Memo1.Lines.Add(XMLDoc.xml);
  Memo1.Lines.Add('');

  XMLDoc.load('C:\ProjektA_TestDB\ICD10\3.xml');
  Memo1.Lines.Add(XMLDoc.xml);
  Memo1.Lines.Add('');
end;

procedure TForm54.Button4Click(Sender: TObject);
var
  xml: IXMLDOMDocument;
  node: IXMLDomNode;
  nodes_row, nodes_se: IXMLDomNodeList;
  nodes_kapitel: IXMLDomNodeList;
  i, j: Integer;
  url: string;
begin
  // put url or file name
  url := 'C:\ProjektA_TestDB\ICD10\icd_2.1_74_tf+2015q1.xml';

  // klappt nicht
  //xml := CreateOleObject('Microsoft.XMLDOM') as IXMLDOMDocument;

  xml := CoDOMDocument.Create;

  xml.async := False;
  //xml.loadXML(url);
  xml.load(url);

  if xml.parseError.errorCode <> 0 then
    raise Exception.Create('XML Load error:' + xml.parseError.reason);

  Memo1.Clear;
  nodes_row := xml.selectNodes('blabla');
  for i := 0 to nodes_row.length - 1 do
  begin
    node := nodes_row.item[i];

    //nodes_kapitel:= node.selectNodes('kapitel


    Memo1.Lines.Add('phrase=' + node.selectSingleNode('phrase').text);
    nodes_se := node.selectNodes('search_engines/search_engine/se_url');
    for j := 0 to nodes_se.length - 1 do
    begin
      node := nodes_se.item[j];
      Memo1.Lines.Add('url=' + node.text);
    end;
    Memo1.Lines.Add('--------------');
  end;end;

end.
Danke

Heiko
Heiko
  Mit Zitat antworten Zitat