Sponsored Content
Top Forums Programming Extracting Field values for XML file Post 302301262 by junaid.nehvi on Thursday 26th of March 2009 10:46:14 AM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
strtok(3)						     Library Functions Manual							 strtok(3)

NAME
strtok, strtok_r - Split string into tokens LIBRARY
Standard C Library (libc.so, libc.a) SYNOPSIS
#include <string.h> char *strtok( char *s1, const char *s2); char *strtok_r( char *s1, const char *s2, char **savept); STANDARDS
Interfaces documented on this reference page conform to industry standards as follows: strtok_r(): POSIX.1c strtok(): XPG4, XPG4-UNIX Refer to the standards(5) reference page for more information about industry standards and associated tags. PARAMETERS
Contains a pointer to the string to be searched. Contains a pointer to the string of byte token delimiters. [POSIX] Identifies the loca- tion of the byte where the search for tokens should be started in the next call to strtok_r(). The savept parameter contains a pointer to a variable that contains a pointer to the byte in the string. DESCRIPTION
The strtok() function splits the string pointed to by the s1 parameter into a sequence of tokens, each of which is delimited by a byte equal to one of the bytes in the s2 parameter. Usually, the strtok() function is called repeatedly to extract the tokens in a string. The first time the application program calls the strtok() function, it sets the s1 parameter to point to the input string. The function returns a pointer to the first token. Then the application program calls the function again with the s1 parameter set to the null pointer. This call returns a pointer to the next token in the string. The application program repeats the call to strtok() with the s1 parameter set to the null pointer until all the tokens in the string have been returned. Note If the input string contains no instances of bytes from the delimiter string, the first call to strtok() results in the return of a pointer to the original string. On subsequent calls, strtok() returns NULL. In the initial call to strtok(), the function first searches the string pointed to by the s1 parameter to locate the first byte that does not occur in the delimiter string pointed to by the s2 parameter. If such a byte is found, it is the start of the first token. The str- tok() function then searches from there for a byte that does occur in the delimiter string. If such a delimiter is found, strtok() over- writes it with a null byte, which terminates the current token. The strtok() function saves a pointer to the byte following the null byte and returns a pointer to the start of the token. In the subsequent calls to strtok(), in which the s1 parameter is set to the null pointer, the function starts at its saved pointer and searches for the next byte that does not occur in the delimiter string pointed to by the s2 parameter. If such a byte is found, it is the start of the new token. The strtok() function then searches from there for a byte that does occur in the delimiter string. If such a delimiter is found, strtok() overwrites it with a null byte, which terminates the new token. The strtok() function saves a pointer to the byte following the null byte and returns a pointer to the start of the new token. If a call to the strtok() function cannot find a byte that does not occur in the delimiter string, it returns the null pointer. If a call to the strtok() function cannot find the terminating byte that does occur in the delimiter string, the current token extends to the end of the string and subsequent calls to strtok() will return the null pointer. If the delimiters used in the string change from one set of characters to another within the string, the application program can set the second parameter, s2, to different strings from call to call. The implementation behaves as though no function calls the strtok() function. The strtok_r() function is the reentrant version of strtok(). Upon successful completion, the strtok_r() function stores the saved pointer in *savept. If the s1 parameter is a null pointer, the strtok_r() function uses the saved pointer in *savept to start searching for the next token. In the initial call to strtok_r(), the *savept must be the null pointer. NOTES
[POSIX] The strtok() function is not supported for multithreaded applications. Instead, its reentrant equivalent, strtok_r(), should be used with multiple threads. EXAMPLES
The following example demonstrates how to split a string into tokens. #include <string.h> #include <locale.h> #include <stdio.h> #define LENGTH 40 main() { char string1[LENGTH], delimiters[LENGTH]; char *pstr ; int counter; (void)setlocale(LC_ALL, ""); printf("Enter the string to be searched: "); if (fgets(string1, LENGTH, stdin) != NULL) { printf("Enter the delimiter(s): "); if (fgets(delimiters, LENGTH, stdin) != NULL) { if ((pstr = strtok(string1, delimiters )) != NULL) { /* pstr points to the first token */ printf("Token 1 is %s ", pstr); counter = 2; while ((pstr = strtok((char *)NULL, delimiters )) != NULL) { printf("Token %d is %s ", counter, pstr); counter++; } } } } } RETURN VALUES
Upon successful completion, the strtok() and strtok_r() functions return a pointer to the first byte of the parsed token in the string. When there is no token in the string, a null pointer is returned. RELATED INFORMATION
Functions: string(3), wcstok(3), wcstok_r(3) Standards: standards(5) delim off strtok(3)
All times are GMT -4. The time now is 12:43 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy