How Check if a XML file if well formed using delphi?

后端 未结 2 898
我在风中等你
我在风中等你 2021-02-06 06:09

How can I check if a xml file is well formed without invalid chars or tags?

For example, consider this xml:




        
2条回答
  •  自闭症患者
    2021-02-06 06:31

    How are you creating/receiving the XML? Any sensible parser would catch this.

    For example, using OmniXML

    uses
      OmniXML;
    
    type
      TForm1=class(TForm)
        Memo1: TMemo;
        //...
      private
        FXMLDoc: IXMLDocument;
        procedure FormCreate(Sender: TObject);
        procedure CheckXML;
      end;
    
    implementation
    
    uses
      OmniXMLUtils;
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      // Load your sample XML. Can also do Memo1.Text := YourXML
      Memo1.Lines.LoadFromFile('YourXMLFile.xml');
    end;
    
    procedure TForm1.CheckXML;
    begin
      FXMLDoc := CreateXMLDoc;
      // The next line raises an exception with your sample file.
      XMLLoadFromAnsiString(FXMLDoc, Memo1.Text); 
    end;
    

提交回复
热议问题