Extracting Field values for XML file


 
Thread Tools Search this Thread
Top Forums Programming Extracting Field values for XML file
# 1  
Old 03-26-2009
Extracting Field values for XML file

i have an input file of XML type with data like

<nx-charging:additional-parameter name="NX_INTERNATIONALIZED_CLID" value="919427960829"/><nx-charging:finalStatus>RESPONSE , Not/Applicable , OK</nx-charging:finalStatus></nx-chargingSmilierocess>

i want to extract data such that i get the output like:

additional-parameter name=NX_INTERNATIONALIZED_CLID
value=919427960829
finalStatus=RESPONSE Not/Applicable , OK


i have done some coding for it

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

int main()
{

char DataIn[] = "<nx-charging:additional-parameter name=\"NX_INTERNATIONALIZED_CLID\" value=\"919427960829\"/><nx-charging:finalStatus>RESPONSE , Not/Applicable , OK</nx-charging:finalStatus></nx-chargingSmilierocess>";

char *str = DataIn;
char *token;
while ( (token = strtok ( str, ":<>/" )) )
{
puts(token);
str = 0;
}
return 0;
}

But my output comes as:

/***OUTPUT*****

nx-charging
additional-parameter name="NX_INTERNATIONALIZED_CLID" value="919427960829"
nx-charging
finalStatus
RESPONSE , Not
Applicable , OK
nx-charging
finalStatus
nx-charging
process




the fields highlighted are not required by me.

Plus the fields

finalStatus
RESPONSE , Not
Applicable , OK

should come as

finalStatus=RESPONSE Not/Applicable , OK

please suggest me some changes.
# 2  
Old 03-26-2009
For starters do not set a pointer to 0 but use NULL instead...so "str = NULL" is good. Also get the token you want and after you get it extract the desired sub-tokens from it. Does it make sense or have I managed to Smilie you utterly.
# 3  
Old 03-27-2009
i have changed the code a little

char DataIn[] = "<nx-charging:additional-parameter name=\"NX_INTERNATIONALIZED_CLID\" value=\"919427960829\"/><nx-charging:finalStatus>RESPONSE , Not/Applicable , OK</nx-charging:finalStatus></nx-chargingSmilierocess>";

char *str = DataIn;
char *token[500];

while ( (token[i] = strtok ( str, "<" )) )
{
puts(token[i]);
i++;
str = NULL;
}


this is how my output looks like

nx-charging:additional-parameter name="NX_INTERNATIONALIZED_CLID" value="919427960829"/>
nx-charging:finalStatus>RESPONSE , Not/Applicable , OK
/nx-charging:finalStatus>
/nx-chargingSmilierocess>


the highlighted fields are not required by me

also " in the output are not required.

and for this out put
finalStatus>RESPONSE , Not/Applicable , OK
i want it to be like
finalStatus=RESPONSE , Not/Applicable , OK

thanks for replies, waiting for more to come.
# 4  
Old 03-27-2009
Code:
int main()
{
    char DataIn[] = "<nx-charging:additional-parameter name=\"NX_INTERNATIONALIZED_CLID\" value=\"919427960829\"/><nx-charging:finalStatus>RESPONSE , Not/Applicable , OK</nx-charging:finalStatus></nx-chargingrocess>";

    int n = 0;
    char *str = DataIn;
    char *token, *subtok;

    while (token = strtok(str, ":" )) {
        ++n;
        if (n == 3) {
            if (subtok = strtok(token, ">"))
                printf("%s=", subtok);
            if (subtok = strtok(NULL, "<"))
                puts(subtok);
        }
        str = NULL;
    }
    return 0;
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Taking key values from one file and extracting values from another file

Hi, I have two files with values in both. File1: cat 2 3 dog 4 5 elephant 6 7 camel 2 3 File2: ----+--gkf;ajf= ---+---- +----- cat -------=----+ 3 | 4 ----- dog ------++-- 5 | 9 ----++-- elephant | 5 | 7 ---++ camel ------ ++++_---- || 8 | 9 I want the final file as: cat 4... (1 Reply)
Discussion started by: npatwardhan
1 Replies

2. Shell Programming and Scripting

Need Help in extracting data from XML File

Hi All My input file is an XML and it has some tags and data rows at end. Starting of data rows is <rs:data> and ending of data rows is </rs:data>. Within sample data rows (2 rows) shown below, I want to extract data value after equal to sign (until space or "/" sign). So if XML data... (7 Replies)
Discussion started by: vx04
7 Replies

3. Shell Programming and Scripting

Extracting the tag name from an xml file

Hi, My requirement is something like this, I have a xml file that contains some tags and nested tags, <n:tag_name1> <n:sub_tag1>val1</n:sub_tag1> <n:sub_tag2>val2</n:sub_tag2> </n:tag_name1> <n:tag_name2> <n:sub_tag1>value</n:sub_tag1> ... (6 Replies)
Discussion started by: Little
6 Replies

4. Shell Programming and Scripting

Comparing delta values of one xml file in other xml file

Hi All, I have two xml files. One is having below input <NameValuePair> <name>Daemon</name> <value>tcp:7474</value> </NameValuePair> <NameValuePair> <name>Network</name> <value></value> </NameValuePair> ... (2 Replies)
Discussion started by: sharsour
2 Replies

5. Shell Programming and Scripting

Reading XML file and extracting value

Dear All, I am reading one XML file to extract value from the particular tag:- Sample xml is below:- <KeyValuePairs> <Key>TestString</Key> <Value>Test12_Pollings</Value> </KeyValuePairs> I want to read the value for the KEY tag and there will be multiple key tags :- awk... (4 Replies)
Discussion started by: sharsour
4 Replies

6. Shell Programming and Scripting

Extracting field values from .csv

How can I select the bold fields from the following? "CLLI","SWREL","RPTDATE","RPTIME","TZ","RPTTYPE","RPTPD","IVALDATE","IVALSTART","IVALEND","NUMENTIDS" "tklc9010801","EAGLE5 45.0.0-64.70.1","2013-08-07","02:01:50","MST ","COMPONENT MEASUREMENTS ON... (4 Replies)
Discussion started by: leghorn
4 Replies

7. UNIX for Dummies Questions & Answers

Extracting values from an XML file

Hello People, I have an xml file from which I need to extract the values of the parameters using UNIX shell commands. Ex : Input is like : <Name>Roger</Name> or <Address>MI</Address> I need the output as just : Roger or MI with the tags removed. Please help. (1 Reply)
Discussion started by: sushant172
1 Replies

8. Shell Programming and Scripting

Extracting tag values from XML using perl

Hi All, I'm trying to extract the values for the 'src' and 'alt' tags within an xml file. In the files that I'm searching, the tags are always enclosed within an 'img' tag. Typically: <img src="diwiz01.gif" width="576" height="254" alt="Out-of-process and In-process COM Objects"><bookmark... (3 Replies)
Discussion started by: Steve_altius
3 Replies

9. Shell Programming and Scripting

Extracting Data from xml file

Hi ppl out there... Can anyone help me with the shell script to extract data from an xml file. My xml file looks like : - <servlet> <servlet-name>FrontServlet</servlet-name> <display-name>FrontServlet</display-name> ... (3 Replies)
Discussion started by: nishana
3 Replies

10. Shell Programming and Scripting

extracting XML file using sed

Hello folks I want to extract data between certain tag in XML file using 'sed' <xml> ......... .......... <one>XXXXXXXXXXXXXXXXXXXX</one> ...... Anyone ?Thank you (7 Replies)
Discussion started by: pujansrt
7 Replies
Login or Register to Ask a Question