Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using C++ to create XML document 2

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello!
I want to use C++ (not MS Visual C++) to create XML documents. I´ve tried something called libxml (gnome) but I´m wondering if there are any more classes/librarys or anything else to do this?

 

Do you know how XML is structured? If so, it's pretty easy to create an XML document "by hand" (i.e. not using a library or a tool). Just use the usual cout or printf statements.

First line should be:
<?xml version=&quot;1.0&quot; ?>&quot;

You can include an encoding section in this to set the characterset encoding used, but leaving it off makes it UTF-8 (which is the XML default).

Next you need to create a root node:

<Customer_Doc>
</Customer_Doc>

Inside the root, create a customer list:
[tab]<Customers>
[tab]</Customers>

In side the customers list elements, create a customer element for each of your customers

[tab][tab]<Customer>
[tab][tab][tab]<LName></LName>
[tab][tab][tab]<FName></FName>
[tab][tab][tab]<Telephone></Telephone>
[tab][tab]</Customer>
[tab][tab]<Customer>
[tab][tab][tab]<LName></LName>
[tab][tab][tab]<FName></FName>
[tab][tab][tab]<Telephone></Telephone>
[tab][tab]</Customer>

And so forth. You can add multiple addresses for your customers and lots of other things.

Another thing you can do is use attributes in some cases instead of elements. For example, the customer element could look like:
<Customer FName=&quot;&quot; LName=&quot;&quot; Telephone=&quot;&quot;>

The big limitation is that attributes can't repeat -- they must be unique. So you can't list multiple addresses this way -- you'd have to nest elements.

Also note that XML (mostly) ignores whitespace outside of the quotes. All of your elements and attributes can be on one huge line. In fact, because you won't have a ton of newlines in there, your file will be smaller and faster to transmit.

I hope this gets you started.

Chip H.
 
Hi, chip!

Nice summary.

This is my solution with awk for generating xml-file from text file with space as field separator:


# to_xml.awk - save data in xml format
# to_xml.awk - croatian: pohrana podataka u xml-zapis
# Kruno Peter, kruno_peter@yahoo.com
# awk, Public Domain, Mar. 2001.
# Jesus loves you.

BEGIN { print &quot;<?xml version=\&quot;1.0\&quot;?>&quot; }

NR == 1 { print &quot;<file filename=\&quot;&quot; FILENAME &quot;\&quot;>&quot; }

{
print &quot;<record>&quot;

for (i = 1; i <= NF; i ++)
print &quot; <fld&quot; i &quot;>&quot; $i &quot;<\/fld&quot; i &quot;>&quot;

print &quot;<\/record>&quot;
}

END { print &quot;<\/file>&quot; }


This awk-source can be placed in a file to_xml.awk and executed by the command:

awk -f to_xml.awk inputfile

For example, if input file with name somedata.txt is
like this:

Krunek awk
Sinisa python

Output will be:

<?xml version=&quot;1.0&quot;?>
<file filename=&quot;somedata.txt&quot;>
<record>
<fld1>Krunek</fld1>
<fld2>awk</fld2>
</record>
<record>
<fld1>Sinisa</fld1>
<fld2>python</fld2>
</record>
</file>


awk is standard unix scripting programming language.

free awk-interpreter (for DOS, Win, Linux) can be found on this site: (GNU, thanks!).

I hope this helps.

Bye!

KP.
 
Krunek -

Thanks for posting. I'm not an &quot;awk&quot; guy, but it's nice to know that the same technique can be applied in other languages/scripts/tools.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top