473,324 Members | 2,214 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,324 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 2215
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.