Einzelnen Beitrag anzeigen

Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
44.184 Beiträge
 
Delphi 12 Athens
 
#338

AW: himXML (gesprochen himix ML)

  Alt 19. Feb 2018, 10:30
Upps, dachte ich hätte schon geantwortet.

Die EAccessViolation konnte ich nicht nachvollziehen.
> himxml_164 und XE3
> heute Abend kann ich nochmal schnell XE und 10.1 testen

Wie sieht dein System denn aus?
> Versionen XML und Delphi

Hab den Test nochmal schnell zusammengeklöppelt
Delphi-Quellcode:
var XML: TXMLFile;
  Node: TXMLNode;
  S: String;
begin
  // EAccessViolation

  XML := TXMLFile.Create;
  try
    Node := XML.RootNode.AddNode('test');
    XML.RootNode.Nodes.Delete(Node);
    XML.SaveToFile('test.xml');
    // <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    // <xml/>
  finally
    XML.Free;
  end;

  // Node "&amp;amp;" statt "&amp;"

  XML := TXMLFile.Create;
  try
    XML.RootNode.AddNode('test').Text := '&';
    XML.SaveToFile('test.xml');
    S := TFile.ReadAllText('test.xml');
    OutputDebugString(PChar(S)); //ShowMessage(S);
    // <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    // <xml>
    // <test>&amp;</test>
    // </xml>
  finally
    XML.Free;
  end;

  XML := TXMLFile.Create;
  try
    XML.LoadFromFile('test.xml');
    S := '"' + XML.RootNode.Node['test'].Text + '"' + sLineBreak
      + '"' + XML.RootNode.Node['test'].Text_S + '"' + sLineBreak
      + '"' + XML.RootNode.Node['test'].Text_D + '"';
    OutputDebugString(PChar(S)); //ShowMessage(S);
    // "&"
    // "&"
    // "&"
  finally
    XML.Free;
  end;

  XML := TXMLFile.Create;
  try
    XML.LoadFromFile('test.xml');
    XML.SaveToFile('test.xml');
  finally
    XML.Free;
  end;

  XML := TXMLFile.Create;
  try
    XML.LoadFromFile('test.xml');
    S := '"' + XML.RootNode.Node['test'].Text + '"' + sLineBreak
      + '"' + XML.RootNode.Node['test'].Text_S + '"' + sLineBreak
      + '"' + XML.RootNode.Node['test'].Text_D + '"';
    OutputDebugString(PChar(S)); //ShowMessage(S);
    // "&"
    // "&"
    // "&"
  finally
    XML.Free;
  end;

  // Attribute "&amp;amp;" statt "&amp;"

  XML := TXMLFile.Create;
  try
    XML.RootNode.AddNode('test').Attributes.Add('attr', '&');
    XML.SaveToFile('test.xml');
    S := TFile.ReadAllText('test.xml');
    OutputDebugString(PChar(S)); //ShowMessage(S);
    // <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    // <xml>
    // <test attr="&amp;"/>
    // </xml>
  finally
    XML.Free;
  end;

  XML := TXMLFile.Create;
  try
    XML.LoadFromFile('test.xml');
    S := '"' + XML.RootNode.Node['test'].Attribute['attr'] + '"';
    OutputDebugString(PChar(S)); //ShowMessage(S);
    // "&"
  finally
    XML.Free;
  end;

  XML := TXMLFile.Create;
  try
    XML.LoadFromFile('test.xml');
    XML.SaveToFile('test.xml');
  finally
    XML.Free;
  end;

  XML := TXMLFile.Create;
  try
    XML.LoadFromFile('test.xml');
    S := '"' + XML.RootNode.Node['test'].Attribute['attr'] + '"';
    OutputDebugString(PChar(S)); //ShowMessage(S);
    // "&"
  finally
    XML.Free;
  end;

  // DateTime

  XML := TXMLFile.Create;
  try
    XML.RootNode.AddNode('test').Text := Now;
    XML.RootNode.AddNode('test').Attributes.Add('attr', Now);
    XML.SaveToFile('test.xml');
    S := TFile.ReadAllText('test.xml');
    OutputDebugString(PChar(S)); //ShowMessage(S);
    // <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    // <xml>
    // <test>2018-02-19T10-05-51.276</test>
    // <test attr="2018-02-19T10-05-51.598"/>
    // </xml>
  finally
    XML.Free;
  end;

  XML := TXMLFile.Create;
  try
    XML.LoadFromFile('test.xml');

    S := '"' + DateTimeToStr(XML.RootNode.Node['test'].Text) + '"' + sLineBreak;
    //S := S + '"' + XML.RootNode.Node['test'].Text + '"' + sLineBreak; // EVariantTypeCastError: Could not convert variant of type (UnicodeString) into type (Date)
    S := S + '"' + XML.RootNode.Node['test'].Text_S + '"' + sLineBreak + sLineBreak;

    S := S + '"' + DateTimeToStr(XML.RootNode.Node['test'].Attribute['attr']) + '"'+ sLineBreak;
    S := S + '"' + XML.RootNode.Node['test'].Attribute['attr'] + '"' + sLineBreak + sLineBreak;

    S := S + '"' + TXHelper.DateTimeToXML(Now, 3) + '"';
    OutputDebugString(PChar(S)); //ShowMessage(S);
    // "19.02.2018 10:14:41"
    // "2018-02-19T10-14-41.622"
    //
    // "30.12.1899"
    // ""
    //
    // "2018-02-19T10-14-48"
  finally
    XML.Free;
  end;
So ging alles.

Diesmal auch das Datum probiert und ...

Zitat:
EVariantTypeCastError: Could not convert variant of type (UnicodeString) into type (Date)
Bug im XE3?
> Der Cast ist ja wohl andersrum, also Variant(DateTime) > String

Und bei den Attributen klappt das Auslesen nicht ... muß aber noch schauen warum.
Mir fiel grade ein warum es nicht ging.
> Attribut im zweiten Node, aber den ersten Node ausgelesen

Delphi-Quellcode:
  // richtig

  XML := TXMLFile.Create;
  try
    XML.LoadFromFile('test.xml');

    S := '"' + DateTimeToStr(XML.RootNode.Node['test'].Text) + '"' + sLineBreak;
    //S := S + '"' + XML.RootNode.Node['test'].Text + '"' + sLineBreak; // EVariantTypeCastError: Could not convert variant of type (UnicodeString) into type (Date)
    S := S + '"' + XML.RootNode.Node['test'].Text_S + '"' + sLineBreak + sLineBreak;

    S := S + '"' + DateTimeToStr(XML.RootNode.Node['test'].Attribute['attr']) + '"'+ sLineBreak;
    S := S + '"' + XML.RootNode.Node['test'].Attribute['attr'] + '"' + sLineBreak + sLineBreak;

    S := S + '"' + DateTimeToStr(XML.RootNode.Nodes[1].Attribute['attr']) + '"'+ sLineBreak;
    //S := S + '"' + XML.RootNode.Nodes[1].Attribute['attr'] + '"' + sLineBreak + sLineBreak; // EVariantTypeCastError: Could not convert variant of type (UnicodeString) into type (Date)
    S := S + sLineBreak;

    //S := S + '"' + DateTimeToStr((XML.RootNode.XPath['test[2]'] as TXMLNode).Attribute['attr']) + '"'+ sLineBreak;
    //S := S + '"' + (XML.RootNode.XPath['test[2]'] as TXMLNode).Attribute['attr'] + '"' + sLineBreak + sLineBreak;

    S := S + '"' + TXHelper.DateTimeToXML(Now, 3) + '"';
    OutputDebugString(PChar(S)); //ShowMessage(S);
    // "19.02.2018 10:28:40"
    // "2018-02-19T10-28-40.633"
    //
    // "30.12.1899"
    // ""
    //
    // "19.02.2018 10:28:40"
    //
    // "2018-02-19T10-28-45"
  finally
    XML.Free;
  end;
$2B or not $2B
  Mit Zitat antworten Zitat