How to check if an XML file, if it is generated using delphi?

How can I check if an XML file is well formed without invalid characters or tags.

for example, consider this xml

<?xml version="1.0"?> <PARTS> <TITLE>Computer Parts</TITLE> <PART> <ITEM>Motherboard</ITEM> <MANUFACTURER>ASUS</MANUFACTURER> <MODEL>P3B-F</MODEL> <COST> 123.00</COST> </PART> <PART> <ITEM>Video Card</ITEM> <MANUFACTURER>ATI</MANUFACTURER> <MODEL>All-in-Wonder Pro</MODEL> <COST> 160.00</COST> </PART> </PARTSx> 

last tag </PARTSx> should be </PARTS>

+6
xml delphi
source share
2 answers

You can use the IXMLDOMParseError interface returned by the MSXML DOMDocument

this interface returns a series of properties that will help you identify the problem.

  • errorCode Contains the error code of the last parsing error. Only for reading.
  • filepos Contains the absolute position of the file where the error occurred. Only for reading.
  • line Indicates the line number containing this error. Only for reading.
  • linepos Contains the position of the character in the line where the error occurred.
  • Cause Describes the cause of the error. Only for reading.
  • srcText Returns the full text of the string containing the error. Only for reading.
  • url Contains the URL of the XML document containing the latest error. Only for reading.

check out these two functions that use MSXML 6.0 (you can also use other versions)

 uses Variants, Comobj, SysUtils; function IsValidXML(const XmlStr :string;var ErrorMsg:string) : Boolean; var XmlDoc : OleVariant; begin XmlDoc := CreateOleObject('Msxml2.DOMDocument.6.0'); try XmlDoc.Async := False; XmlDoc.validateOnParse := True; Result:=(XmlDoc.LoadXML(XmlStr)) and (XmlDoc.parseError.errorCode = 0); if not Result then ErrorMsg:=Format('Error Code : %s Msg : %s line : %s Character Position : %s Pos in file : %s', [XmlDoc.parseError.errorCode,XmlDoc.parseError.reason,XmlDoc.parseError.Line,XmlDoc.parseError.linepos,XmlDoc.parseError.filepos]); finally XmlDoc:=Unassigned; end; end; function IsValidXMLFile(const XmlFile :TFileName;var ErrorMsg:string) : Boolean; var XmlDoc : OleVariant; begin XmlDoc := CreateOleObject('Msxml2.DOMDocument.6.0'); try XmlDoc.Async := False; XmlDoc.validateOnParse := True; Result:=(XmlDoc.Load(XmlFile)) and (XmlDoc.parseError.errorCode = 0); if not Result then ErrorMsg:=Format('Error Code : %s Msg : %s line : %s Character Position : %s Pos in file : %s', [XmlDoc.parseError.errorCode,XmlDoc.parseError.reason,XmlDoc.parseError.Line,XmlDoc.parseError.linepos,XmlDoc.parseError.filepos]); finally XmlDoc:=Unassigned; end; end; 
+12
source share

How do you create / get XML? Any sensible parser will catch that.

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; 
+5
source share

All Articles