473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Creating an XML document ?

Hi ,

writing a small application in C which has to create an XML document by
reading a binary format file. Trying to get the indentation right by
using a depth global variable which will insert appropriate number of
tab spaces depending up on the depth.

Is there any way that this can be done using a macro.

So , something like

#define INSERTTAB (x) /* not sure what will come here */

INSERTTAB (depth)

thanks in advance,
vivekian

Apr 27 '06 #1
5 2231
vi********@gmail.com wrote:
Hi ,

writing a small application in C which has to create an XML document by
reading a binary format file. Trying to get the indentation right by
using a depth global variable which will insert appropriate number of
tab spaces depending up on the depth.

Is there any way that this can be done using a macro.

Why a macro, what's wrong with a function?

--
Ian Collins.
Apr 27 '06 #2
vi********@gmail.com wrote:
writing a small application in C which has to create an XML document by
reading a binary format file. Trying to get the indentation right by
using a depth global variable which will insert appropriate number of
tab spaces depending up on the depth.


Why not just use a library that already handles creating an XML
rather than re-inventing the wheel? XML is complicated enough that
it's unlikely you're producing valid XML anyway.

For what it's worth, if you really have to do it yourself manually
for some reason, the simplest way is to create a tree as an intermediate
form. Then walk the tree and spit out open tags when you first visit
a node and close tags when you leave the node. (That ignores the
"<foo/>"-style tags where in effect the open and close tags are
combined, but that's a simple extension.)

- Logan
Apr 27 '06 #3
On 2006-04-27, vi********@gmail.com <vi********@gmail.com> wrote:
Hi ,

writing a small application in C which has to create an XML document by
reading a binary format file. Trying to get the indentation right by
using a depth global variable which will insert appropriate number of
tab spaces depending up on the depth.

Is there any way that this can be done using a macro.

So , something like

#define INSERTTAB (x) /* not sure what will come here */

INSERTTAB (depth)


The only reason I can think of for a macro (rather than a function) is
because you want a macro that expands to a string literal:

printf(INSERTTAB(n)"%s\n", tag);

which needs to expand to:

printf("\t\t\t\t""%s\n", "hello");

I'm fairly sure there's no way to do this-- even if the C preprocessor
had more functionality, n isn't known until runtime, so it's got to be
runtime code that "prints" the tabs one way or another.

You could try:

printf("%s%s\n", make_tabs(n), tag);

where make_tabs returns a string of tabs. But then you have to worry
about allocating strings. There are various options, but none of them
are very nice if the indent level gets high.

So a function to actually put the tabs in is your best bet I would say:

void insert_tabs(FILE *fp, unsigned n)
{
...
}

insert_tabs(stdout, 4);
printf("%s\n", tag);

The other option you've got is perhaps generate the xml without any
indentation at all, and then pipe it through one of the many xml
indentation programs that exist-- the xslt program to do this is
practically a one-liner.
Apr 27 '06 #4

In article <sl*********************@bowser.marioworld>, Ben C <sp******@spam.eggs> writes:
On 2006-04-27, vi********@gmail.com <vi********@gmail.com> wrote:

writing a small application in C which has to create an XML document by
reading a binary format file. Trying to get the indentation right by
using a depth global variable which will insert appropriate number of
tab spaces depending up on the depth.

Is there any way that this can be done using a macro.

So , something like

#define INSERTTAB (x) /* not sure what will come here */

INSERTTAB (depth)
The only reason I can think of for a macro (rather than a function) is
because you want a macro that expands to a string literal:

printf(INSERTTAB(n)"%s\n", tag);

which needs to expand to:

printf("\t\t\t\t""%s\n", "hello");

I'm fairly sure there's no way to do this-- even if the C preprocessor
had more functionality, n isn't known until runtime, so it's got to be
runtime code that "prints" the tabs one way or another.


You're insufficiently perverse.

-----
#include <stdio.h>

#define INSERTTAB(n) (8-(n))+"\t\t\t\t\t\t\t\t"

int main(void)
{
int i;
for (i=0; i<=8; i++)
puts(INSERTTAB(i) "*");

return 0;
}
-----

Obviously this is fragile, limited, and generally awful, but it does
work for the specific case you cited.
You could try:

printf("%s%s\n", make_tabs(n), tag);

where make_tabs returns a string of tabs. But then you have to worry
about allocating strings. There are various options, but none of them
are very nice if the indent level gets high.


Here I'd almost be ready to recommend a macro that indexed into a
long constant string of tabs. No need to allocate anything, if you
can set a maximum on your indentation level. You could also limit
the indentation level in the macro, though that requires evaluating
the argument more than once except for some restricted cases (eg
where you can use binary-and or a similar operation to truncate it).

--
Michael Wojcik mi************@microfocus.com

The lark is exclusively a Soviet bird. The lark does not like the
other countries, and lets its harmonious song be heard only over the
fields made fertile by the collective labor of the citizens of the
happy land of the Soviets. -- D. Bleiman
May 2 '06 #5
On 2006-05-02, Michael Wojcik <mw*****@newsguy.com> wrote:

In article <sl*********************@bowser.marioworld>, Ben C <sp******@spam.eggs> writes:
On 2006-04-27, vi********@gmail.com <vi********@gmail.com> wrote:
>
> writing a small application in C which has to create an XML document by
> reading a binary format file. Trying to get the indentation right by
> using a depth global variable which will insert appropriate number of
> tab spaces depending up on the depth.
>
> Is there any way that this can be done using a macro.
>
> So , something like
>
> #define INSERTTAB (x) /* not sure what will come here */
>
> INSERTTAB (depth)


The only reason I can think of for a macro (rather than a function) is
because you want a macro that expands to a string literal:

printf(INSERTTAB(n)"%s\n", tag);

which needs to expand to:

printf("\t\t\t\t""%s\n", "hello");

I'm fairly sure there's no way to do this-- even if the C preprocessor
had more functionality, n isn't known until runtime, so it's got to be
runtime code that "prints" the tabs one way or another.


You're insufficiently perverse.

-----
#include <stdio.h>

#define INSERTTAB(n) (8-(n))+"\t\t\t\t\t\t\t\t"

int main(void)
{
int i;
for (i=0; i<=8; i++)
puts(INSERTTAB(i) "*");

return 0;
}
-----

Obviously this is fragile, limited, and generally awful, but it does
work for the specific case you cited.


Most ingenious!
You could try:

printf("%s%s\n", make_tabs(n), tag);

where make_tabs returns a string of tabs. But then you have to worry
about allocating strings. There are various options, but none of them
are very nice if the indent level gets high.


Here I'd almost be ready to recommend a macro that indexed into a
long constant string of tabs. No need to allocate anything, if you
can set a maximum on your indentation level.


I did think of that one, and lumped it with "solutions that aren't very
nice if the indent level gets high".
May 3 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Kerri McDonald | last post by:
We have an application where the user fills out many screens and when they are done, we are supposed to display the text they entered in a word or excel format. That is fairly easily accomplished...
20
by: svend | last post by:
I'm messing with some code here... Lets say I have this array: a1 = ; And I apply slice(0) on it, to create a copy: a2 = a1.slice(0); But this isn't a true copy. If I go a1 = 42, and then...
7
by: Russ | last post by:
Hi All, I have a problem getting the following simple example of "document.write" creating a script on the fly to work in all html browsers. It works in I.E., Firefox, and Netscape 7 above. It...
2
by: pshvarts | last post by:
(I'm new in SOAP) I get some wsdl file (from apache service ). I tried creating SOAP client with .NET - trying to add Web Reference and get error like: "Custom tool error: Unable to import...
6
by: Adam Tilghman | last post by:
Hi all, I have found that IE doesn't seem to respect the <SELECT> "multiple" attribute when set using DOM methods, although the attribute/property seems to exist and is updated properly. Those...
5
by: sam | last post by:
Hi all, I am dynamically creating a table rows and inerting radio buttons which are also dynamically created. Everything works fine in Firefox as expected. But I am not able to select radio...
4
by: GRenard | last post by:
Hi, I'm trying just to display a table on a webpage using DOM elements created dynamically. I really don't understand why IE doesn't display the document successfully... If I make a...
3
by: patrickkellogg | last post by:
I have this code when you click the buttom is suppose to add a job history. it works with firefox, opera, but not ie. (please note - new entries don't have all the elements in them yet, but...
1
by: skyson2ye | last post by:
Hi, guys: I have written a piece of code which utilizes Javascript in PHP to create a three level dynamic list box(Country, States/Province, Market). However, I have encountered a strange problem,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.