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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I Extract strings within strings 2

Status
Not open for further replies.

anushrestha

Programmer
Jun 25, 2007
7
US

I have a xml doc, I need to parse certain tag. For ex. I need to extract strings between <Transaction> and </Transaction>..
How would i achieve that?

Thank you


<OrderFormDoc>
.........
<Transaction>
<CurrentTotals>
<Totals>
<AltTax DataType="Money" Currency="840">0</AltTax>
<DiscAmt DataType="Money" Currency="840">0</DiscAmt>
<DutyTax DataType="Money" Currency="840">0</DutyTax>
<Ship DataType="Money" Currency="840">500</Ship>
<StateTax DataType="Money"Currency="840">0</StateTax>
<Total DataType="Money" Currency="840">5499</Total>
</Totals>
</CurrentTotals>
<InputEnvironment DataType="S32">4</InputEnvironment>
<InvNumber DataType="String">2685667</InvNumber>
<PoNumber DataType="String">2685667</PoNumber>
</Transaction>
......
</OrderFormDoc>
 
Thanks V
I did check strtok funciton. But it extract string using a delimiters (a single char). My problem is to extract string
between
<Transaction>
....
</Transaction>


 
Ah, right. I would probably just go through using strstr() searching alternately for the opening and closing tags, then.


-V
 
Thanks V,
I have been trying to use that strstr() function for past 2days, I just cannot make it work.

Do you know of any example of C code out there that will achieve what I am trying to do?

Thank you
 
Given that it is an XML document, would it not be easier just to use an XML parser (eg to do all the donkey work of parsing, then all you have to deal with is a couple of callback functions which receive the elements and attributes of the document.



--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
I agree with Salem, that an XML parser is probably a better way to go. If this is not an option, you can try something like the following, although admitted my C string syntax is extremely rusty and this probably doesn't work at all :p

Code:
char* xmlData; //Your data
char* startTag = "<Transaction>";
char* endTag = "</Transaction>";
char* startLocation;
char* endLocation;
char* tempString;
int tempLength;

while(strstr(xmlData,startTag)!=NULL)
{
    startLocation = strstr(xmlData,startTag)+strlen(startTag); //Find the beginning of your string
    endLocation = strstr(xmlData,endTag); //Find the end of your string
    tempLength = endLocation - startLocation; //Calculate the length of the string
    strncpy(tempString,startLocation,tempLength); //Extract your string
    
    // Do something with your string

    xmlData = endLocation; //Move on in the XML Data
}


-V
 
V
Beleive or not I did similer to this that you sent me and got to the point of core dump. If you complie the code you gave me, it does core dump also at the same place strncpy. It just doesn't like the string pointers and I have string pointer just like below. So I am trying to move from the string pointer to the array with the for loop. With strncpy array is working not the string pointer. Any idea???
Thank you very very much for trying to help.



#include <string.h>
#include <stdio.h>

int main(void)
{

char* xmlData = "<Hello> this is a <test> site of the company </test> </Hello> "; //Your data
char* startTag = "<test>";
char* endTag = "</test>";
char* startLocation;
char* endLocation;
char* tempString=NULL;
int tempLength;

while(strstr(xmlData,startTag)!=NULL)
{
startLocation = strstr(xmlData,startTag)+strlen(startTag); //Find the beginning of your string
endLocation = strstr(xmlData,endTag); //Find the end of your string
tempLength = endLocation - startLocation; //Calculate the length of the string
printf(" Start Loc = %s\n",startLocation);
printf("tempLength = %d\n",tempLength);
strncpy(tempString,startLocation,tempLength); //Extract your string
}
}
 
I need to allocate the memory to tempString before doing strncpy and it works now..

Thank you all
Thanks V
 
Thanks all! This solved a problem for me too!
That's why I love this forum!

Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top