[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2289
  • Last Modified:

"EDomParse error: Invalid at the top level of the document".

HI,

  I used TXMLDocument component to load and parse the xml file. I want to validate that xml file before parsing with xsd file. for this i set the parse options for xmldocument1 like below

xmldocument1 .ParseOptions := xmldocument1 .ParseOptions + [poValidateOnParse];

after that i used below statements

 xmldocument1 .LoadFromXML(filename);
  xmldocument1 .Active;

when i tried to exute my program i am getting the following error.

"EDomParse error: Invalid at the top level of the document".

can any body give the suggestions on this?
0
sbondalapati
Asked:
sbondalapati
  • 10
  • 7
1 Solution
 
epasquierCommented:
what is you XML file ?
0
 
ebob42Commented:
Perhaps the first line of the XML document contains the <?xml ...> tag that needs to be removed?

Alternately, here's another way to validate an XML model against its schema. This can be done with the validateOnParse property of the MSXML.DOMDocument, as follows:


  var
    XML, XSDL: Variant;
  begin
    XSDL := CreateOLEObject('MSXML2.XMLSchemaCache.4.0');
    XSDL.validateOnLoad := True;
    XSDL.add('','MySchema.xsd'); // 1st argument is target namespace
    ShowMessage('Schema Loaded');
    XML := CreateOLEObject('MSXML2.DOMDocument.4.0');
    XML.validateOnParse := True;
    XML.resolveExternals := True;
    XML.schemas := XSDL;
    XML.load('file.xml');
    ShowMessage(XML.parseError.reason);
  end.

Open in new window

0
 
sbondalapatiAuthor Commented:
hi,

  I am attaching xml file.
Updateinformation.xml
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
ebob42Commented:
Try to remove the first line with <?xml version="1.0" encoding="UTF-8"?>
0
 
ebob42Commented:
Do you also have a corresponding xmlFileValidation.xsd which is mentioned in the first actual XML line for the xsi:noNamespaceSchemaLocation ?
0
 
ebob42Commented:
With my XML2XSD tool, I can generate a XSD file as follows:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="updateinformation" type="updateinformationType"/>
  <xs:complexType name="updateinformationType">
    <xs:sequence>
      <xs:element name="UpdateNumber" type="xs:integer"/>
      <xs:element name="ManufacturerCode" type="xs:string"/>
      <xs:element name="ExeVerNumber" type="xs:integer"/>
      <xs:element name="DBStructVerNumber" type="xs:integer"/>
      <xs:element name="PrevExeVerNumber" type="xs:integer"/>
      <xs:element name="PrevDBStructVerNumber" type="xs:integer"/>
      <xs:element name="PrevUpdateNumber" type="xs:integer"/>
      <xs:element name="Mandatory" type="xs:string"/>
      <xs:element name="Description" type="xs:string"/>
      <xs:element name="UpdateType" type="xs:string"/>
      <xs:element name="FilesToDownload" type="FilesToDownloadType"/>
    </xs:sequence>
    <xs:attribute name="noNamespaceSchemaLocation" type="xs:string"/>
  </xs:complexType>
  <xs:complexType name="FilesToDownloadType">
    <xs:sequence>
      <xs:element name="F1" type="xs:string"/>
      <xs:element name="F2" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

Open in new window

0
 
ebob42Commented:
And then I can pass your XML document and the generated XSD to the following program, which tells me the XML is valid:
program ValidateXML;
{$APPTYPE CONSOLE}
uses
  SysUtils,
  Ole2, OleAuto;

var
  XML, XSDL: Variant;
begin
  if ParamCount = 0 then
  begin
    writeln('Usage: ValidateXML XML XSD');
    writeln
  end
  else // validate
  try
    XSDL := CreateOLEObject('MSXML2.XMLSchemaCache.4.0');
    XSDL.validateOnLoad := True;
    XSDL.add('',ParamStr(2)); // 1st argument is target namespace
    writeln('Schema Loaded');
    XML := CreateOLEObject('MSXML2.DOMDocument.4.0');
    XML.validateOnParse := True;
    XML.resolveExternals := True;
    XML.schemas := XSDL;
    XML.load(ParamStr(1));
    writeln(XML.parseError.reason);
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Open in new window

0
 
sbondalapatiAuthor Commented:
HI Ebob,

   Thank you for your example. Can you give me an example in which TxmlDocument component is used and parseoptions are set. like the below
  xmlDocu: TXMLDocument;
xmlDocu.ParseOptions := xmlDocu.ParseOptions + [poValidateOnParse];
xmlDocu.LoadFromXML(xmlFileName);
  xmlDocu.Active;

if you gave this it will be very helpful for me

thanks
0
 
ebob42Commented:
It was just a matter of incorrect loading the file. Your call to "Active" is not a call, it's a property.

The following code works for me: calling LoadFromFile and then checking the Active property, or setting the FileName and then Active to True.
procedure TExpertExchangeForm.Button5Click(Sender: TObject);
const
  xmlFileName = 'c:\Updateinformation.xml';
begin
  xmlDocu.ParseOptions := xmlDocu.ParseOptions + [poValidateOnParse];
//xmlDocu.FileName := xmlFileName;
//xmlDocu.Active := True;
  xmlDocu.LoadFromFile(xmlFileName);
  if xmlDocu.Active then ShowMessage('OK');
end;

Open in new window

0
 
sbondalapatiAuthor Commented:
HI ebob,

   I am getting "EOLeSysError : Invalid class string" error when i used your code at the line stated below

XSDL := CreateOLEObject('MSXML2.XMLSchemaCache.4.0');

thanks
0
 
sbondalapatiAuthor Commented:
HI ebob,

  i wrote the same code but for me its throwing error that "iInvalid at the top level of the document".

I am using
  MSXML 6.0 parser, windows xp sp2 and Delphi 2010.

is there anything related to them.
0
 
ebob42Commented:
You could try a different DOMVendor (see the DOMVendor property of the TXMLDocument component).

I'm also using Delphi 2010 on WIndows XP here. It works for me ;-)
0
 
sbondalapatiAuthor Commented:
HI,

  I am using MSXML as domvendor. can you tell me the domvendor you used? Becuase i am not able to run mycode still i am getting same errors as stated above.

thanks
0
 
ebob42Commented:
I also used MSXML...
0
 
sbondalapatiAuthor Commented:
which version of MSXML?
0
 
ebob42Commented:
I see MSXML 4.0 SP2 as well as 6.0 Parser on my system.
0
 
ebob42Commented:
Try playing with the ParseOptions. Can you open the document with empty ParseOptions, or with only the ValidateOnParse?
  xmlDocu.ParseOptions := [poValidateOnParse];

Open in new window

0
 
sbondalapatiAuthor Commented:
HI Ebob,

   You did an excellent job. Thanks for the help.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 10
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now