Substring between XML

Hey Everyone,

Does anyone know if its possible to perform a substr on a value between an xml node

i.e.

<field="tmp">replace me</field>

I need to be able replace the value inside the tag element with a char *

thanks,

Rob.

I take it you're programming in C?

Of course it's possible, but C isn't exactly a string language, it doesn't have the operators or memory management to make that easy. You can't assume there's enough memory in your array to hold the string you want to put there for instance, and can't assume that the array was even allocated in the first place(what if it's an array on the stack).

What does your input look like? Just the single line like that, or does it have to hunt through a big mess of tags by itself too? And what's it being held in?

Im looping through an xml file and using fgets on the line storing it in a char *thisline[1024]. There will only ever be one tag on each line.

It being its own line makes it a lot easier.

char buf[1024];
char *token="<field=\"tmp\">", *match;

strcpy(buf, "<field=\"tmp\">replace me</field>");

// Gives us NULL if no match, or the position of the first match
match=strstr(buf, token);
if(match)
{
        // Move past the token
        match+=strlen(token);
        // Overwrite EVERYTHING after the token
        sprintf(match, "%s</field>\n", "replacement value");
}
1 Like

Hey Corona688, thanks for you help.

So i now have:-

//Global Declarations
char sSearchString[1024] = {0,};
char *result;
FILE *istream;
while(!feof(istream)) //main loop
{
	char this_line[256];
	fgets (this_line, sizeof(this_line), istream);
	sprintf(sSearchString, "<field name=\"tmp\">");

	result=strstr(this_line, sSearchString);
	if(result) //we are in the media table
	{
		result+=strlen(sSearchString);
		sprintf(result, "%s</field>\n", newtmp);
		sprintf(this_line, "%s", result);
	}

	//print to new file
	FILE *fout = fopen("new.xml","a");
	if(fout == NULL)
	{
		fclose(fout);
		return false;
	}
	fprintf(fout,"%s",this_line);
	fclose(fout);
}

I am no longer seeing <field name="tmp"> in my string that i replace. Im only seeing "newvalue</field>".

Thanks again,

Rob.

I posted bs here, please delete ^^

Why did you do this?

sprintf(this_line, "%s", result);

That undoes the work of the previous line. Take it out.

Regards,
Mark.

//populate result
sprintf(result, "%s</field>\n", newtmp);
//copy result to this_line and output to file at later time.
sprintf(this_line, "%s", result);

I also need to be able to keep all tab spacing before the <field name

---------- Post updated at 01:18 PM ---------- Previous update was at 01:10 PM ----------

I done that because i print out this_line to the new file. Also, i would like to keep the xml formatting in place i.e. tabs etc

But result is a pointer into this_line.

sprintf(result, "%s</field>\n", newtmp);

... is already updating this_line. Check the contents of this_line immediately after the above statement, you should find that it contains exactly what you need.

1 Like

hahaha how did i miss that. Thanks everyone, really appreciate your help :slight_smile: