Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
xml2c(1)		     General Commands Manual			xml2c(1)

NAME
     xml2c - convert an XML file into C struct/string declarations

SYNOPSIS
     xml2c xmlfile

DESCRIPTION
     xml2c  reads in an XML file and produces equivalent C source code, suitable
     for #including in your program.  The C version is probably easier deal with
     in your code.  You don't have to read and parse the XML file and then  look
     for  the  nodes you want; instead you just loop through a bunch of structs.
     Or maybe you just don't want to distribute extra files with your  app,  and
     would rather build them into the executable.

     Example input:
     <?xml version="1.0" encoding="utf-8" ?>
     <list>
       <item id="1" value="17">this item</item>
       <item id="2" value="23">that item</item>
       <item id="3" value="42">the other item</item>
     </list>

     Example output:
     % xml2c sample.xml
     struct _item {
	 char* id;
	 char* value;
	 char* TEXT;
	 };
     struct _list {
	 struct _item item[3];
	 int N_item;
	 };
     static struct _list list = {
	 item: { {
	     id: "1",
	     value: "17",
	     TEXT: "this item",
	     }, {
	     id: "2",
	     value: "23",
	     TEXT: "that item",
	     }, {
	     id: "3",
	     value: "42",
	     TEXT: "the other item",
	     } },
	 N_item: 3,
	 };

PROBLEMS
     xml2c  should be able to produce output for any valid XML input file.  How-
     ever, there are some valid XML inputs for which the C output will not  com-
     pile.   For  example,  if	there are XML elements with the same name but in
     different contexts, xml2c may not detect that they are the  same  and  will
     produce two different struct definitions with the same name.  That will re-
     sult  in  a  C  compiler error, even if the two structs have the exact same
     fields:
     % cat bogus.xml
     <?xml version="1.0" encoding="utf-8" ?>
     <foo>
       <item>this item</item>
       <bar>
	 <item>that item</item>
       </bar>
     </foo>
     % xml2c bogus.xml > bogus.c
     % cat bogus.c
     struct _item {
	 char* TEXT;
	 };
     struct _item {
	 char* TEXT;
	 };
     struct _bar {
	 struct _item item;
	 int N_item;
	 };
     struct _foo {
	 struct _item item;
	 int N_item;
	 struct _bar bar;
	 int N_bar;
	 };
     static struct _foo foo = {
	 item: {
	     TEXT: "this item",
	     },
	 N_item: 1,
	 bar: {
	     item: {
		 TEXT: "that item",
		 },
	     N_item: 1,
	     },
	 N_bar: 1,
	 };
     % gcc bogus.c
     bogus.c:4: redefinition of `struct _item'

     Another case that can result in C compiler errors is if you use #include to
     bring multiple xml2c result files into a single .c file.  If two or more of
     the XML files have elements with the same name, then the result files  will
     have  multiple  struct  definitions  with the same name, and again that's a
     compiler error.

AUTHOR
     Copyright (C) 2011 by Jef Poskanzer  <jef@mail.acme.com>.	All  rights  re-
     served.

				15 September 2011			xml2c(1)

Want to link to this manual page? Use this URL:
<https://man.freebsd.org/cgi/man.cgi?query=xml2c&sektion=1&manpath=FreeBSD+Ports+15.1.quarterly>

home | help