Tuesday, May 26, 2009

Validate XML against XSD in C

The XML documents can reference schema documents that specify how the XML documents should be structured. The rules for validating the XML document is specified in the schema which use the extension .xsd For more information about W3C XML Schema click here

In this Tutorial I'll show how to use libxml2 to Validate XML documents.

Libxml2 is the XML C parser and toolkit developed for the Gnome project (but usable outside of the Gnome platform), it is free software available under the MIT License. XML itself is a meta language to design markup languages, i.e. text language where semantic and structure are added to the content using extra "markup" information enclosed between angle brackets. HTML is the most well-known markup language. Though the library is written in C a variety of language bindings make it available in other environments.

Libxml2 is known to be very portable, the library should build and work without serious troubles on a variety of systems (Linux, Unix, Windows, CygWin, MacOS, MacOS X, RISC Os, OS/2, VMS, QNX, MVS, ...)

Sample program in C to validate xml against xsd using libxml2

Environment & Settings:
* Install libxml2 2.6.32 binary [Installed by default in most of the Linux].

* Install libxml2-dev 2.6.32 package.

* IDE Eclipse CDT [GCC Compiler]

* Right click the project --> Properties --> C/C++ Build --> Settings

select Tool Settings --> GCC C Linker --> Libraries
Add "xml2"(without quotes) in Libraries

select GCC C Compiler --> Directories --> Include Paths
Add "/usr/include/libxml2" (without quotes)

* This sample program uses test.xml and test.xsd

Code: xmlvalidation.c


you can also validate XML against XSD using the following command in linux.

xmllint --noout --schema test.xsd test.xml



Anonymous said...

thanks its useful

Anonymous said...

Thanks a million! Worked like a charm.

Anonymous said...

Thank you very much... it works without any problems :)

Abhishek Sudhakaran said...

any example on C++ bindings???

Anonymous said...

thanks. it is working for me also.


Anonymous said...

wow !! this is so osmple thaks a lot

Phong Nguyen said...

Thank a lots! It's really helpful to me. But can you include project and attach it to this post? Thank you very much

Zeeshan Mahmood said...

Great example. It worked super fine on VS2010 as well.

Post a Comment

Tamil News Live Online HD

Tamil News Live Online HD