Sponsored Content
Full Discussion: Parse XML line
Top Forums Shell Programming and Scripting Parse XML line Post 302654961 by Corona688 on Tuesday 12th of June 2012 01:18:34 PM
Old 06-12-2012
Code:
$ cat xmlline.sh
#!/bin/bash

# Remove temp file on exit
trap "rm -f /tmp/$$" EXIT

# Little-known incredibly useful property of xargs:  it handles quotes.
# We can use it to transform x='a' y='b c d' into lines like
#
#       x=a
#       y=b c d
#       ...
#
# Ordinary 'while read' loop can handle that easily, splitting on the first =.
xargs printf '%s\n' < xmldata > /tmp/$$

while IFS="=" read NAME VALUE
do
        END=0
        if [ "${VALUE/\/>/}" != "$VALUE" ]      # Check for /> end-of-token
        then
                END=1
                VALUE="${VALUE/\/>/}"
        fi

        [ "${NAME:0:1}" = "<" ] && NAME="${NAME:1}"     # Remove <

        # Sanity check, don't assign variables when we have no value
        if [ ! -z "$VALUE" ]
        then
                VARS="$VARS $NAME"
                read $NAME <<<"$VALUE" # BASH trick to assign variable name without eval
        fi

        # If we found />, print and clear all variables
        if [ "$END" -gt 0 ]
        then
                echo "Found fields $VARS"
                for X in $VARS
                do
                        printf "$X\t${!X}\n"
                        read $X <<<"" # BASH trick to clear variable name without eval
                done
                echo
                VARS=""
        fi
done < /tmp/$$

exit 0

$ ./xmlline.sh
Found fields  name title add color ls lw normf xsection EqLumi filenames
name    T2tt_350_100
title   T2tt_012j_350_100_428p4_pPF_PU
add     1
color   4
ls      1
lw      2
normf   1
xsection        0.070152
EqLumi  94651.6
filenames       /user/alkaloge/files/TopTree/7TeV/T2tt_2j_FSim_428p4pPF_S11_PU_350_100/T2tt_2j_FSim_428p4pPF_S11_PU_350_100.root

Found fields  name title add color ls lw normf xsection EqLumi filenames
name    T2tt_500_100
title   T2tt_012j_500_100_428p4_pPF_PU
add     1
color   4
ls      1
lw      2
normf   1
xsection        0.006867
EqLumi  936653.56
filenames       /user/alkaloge/files/TopTree/7TeV/T2tt_2j_FSim_428p4pPF_S11_PU_500_100/T2tt_2j_FSim_428p4pPF_S11_PU_500_100.root


$

This User Gave Thanks to Corona688 For This Post:
 

10 More Discussions You Might Find Interesting

1. Programming

parse xml

Hi, I'm looking for an "easy" way to parse a xml file to a proper structure. The xml looks like this What shall I use? Does anybody has some example-code to share or some good links/book-references? thx for any reply -fe (5 Replies)
Discussion started by: bin-doph
5 Replies

2. Shell Programming and Scripting

How to parse a XML file using PERL and XML::DOm

I need to know the way. I have got parsing down some nodes. But I was unable to get the child node perfectly. If you have code please send it. It will be very useful for me. (0 Replies)
Discussion started by: girigopal
0 Replies

3. Shell Programming and Scripting

How can I parse xml file?

How can I parse file containing xml ? I am sure that its best to use perl - but my perl is not very good - can someone help? Example below contents of file containing the xml - I basically want to parse the file and have each field contained in a variable.. ie. I want to store the account... (14 Replies)
Discussion started by: frustrated1
14 Replies

4. Shell Programming and Scripting

Parse an XML task list to create each task.xml file

I have an task definition listing xml file that contains a list of tasks such as <TASKLIST <TASK definition="Completion date" id="Taskname1" Some other <CODE name="Code12" <Parameter pname="Dog" input="5.6" units="feet" etc /Parameter> <Parameter... (3 Replies)
Discussion started by: MissI
3 Replies

5. Emergency UNIX and Linux Support

How to parse the following xml file

Hi, I have the following file Example.xml <?xml version="1.0" encoding="iso-8859-1"?> <html><set label="09/07/29" value="1241.90"/> </html> Can any one help me in parsing this xml file I want to retrive the attribute values of the tag set Example I want to... (3 Replies)
Discussion started by: Raji_gadam
3 Replies

6. Shell Programming and Scripting

parse xml file

Hello all, Given the following extract from a xml file with multiple <JOB> .... </JOB> entries <JOB APPLICATION="APP" APR="0" AUG="0" AUTHOR="AUT" AUTOARCH="0" CMDLINE="/tmp/test1 %%var" CONFIRM="1" CREATION_DATE="20100430" CREATION_TIME="130739" ... (2 Replies)
Discussion started by: cabrao
2 Replies

7. Shell Programming and Scripting

Parse XML

Hi all! I'm looking to write a quick script and in it I need to request an XML file from a service running on localhost and parse that XML file and output it. I'm looking to do it in bash although it doesn't really matter what shell it is in. The XML file returned would look like this: ... (3 Replies)
Discussion started by: mtehonica
3 Replies

8. Shell Programming and Scripting

Parse XML File.

HI Guys I have Below XML File : <xn:SubNetwork id="XYZ"> <xn:SubNetwork id="C01"> <xn:MeContext id="CO1"> <xn:ManagedElement id="1"> <un:RncFunction id="1"> <un:UtranCell id="NY431"> ... (2 Replies)
Discussion started by: pareshkp
2 Replies

9. Shell Programming and Scripting

Parse xml file

I am trying to create a shell script that will parse an xml file (file attached). awk '/Id v=/ { print }' Test.xml | sed 's!<Id v=\"\(.*\)\"/>!\1!' > output.txt An output.txt file is created but it is empty. It should contain the value 222159 in it. Thanks. (7 Replies)
Discussion started by: cmccabe
7 Replies

10. Shell Programming and Scripting

Parse XML For Values

Hi All, I want to parse XML to extract values of the tags to do further processing. The XML looks like <?xml version="1.0" encoding="ISO-8859-1"?> <allinput> <input A="2389906" B="install"> <C>111</C> <D>222</D> <E>333</E> <F></F> <G>444</G> <H></H> <I></I> <J></J> <K>C,D,E,G</K>... (6 Replies)
Discussion started by: rahulmittal87
6 Replies
XML_SET_OBJECT(3)							 1							 XML_SET_OBJECT(3)

xml_set_object - Use XML Parser within an object

SYNOPSIS
bool xml_set_object (resource $parser, object &$object) DESCRIPTION
This function allows to use $parser inside $object. All callback functions could be set with xml_set_element_handler(3) etc and assumed to be methods of $object. PARAMETERS
o $parser - A reference to the XML parser to use inside the object. o $object - The object where to use the XML parser. RETURN VALUES
Returns TRUE on success or FALSE on failure. EXAMPLES
Example #1 xml_set_object(3) example <?php class xml { var $parser; function xml() { $this->parser = xml_parser_create(); xml_set_object($this->parser, $this); xml_set_element_handler($this->parser, "tag_open", "tag_close"); xml_set_character_data_handler($this->parser, "cdata"); } function parse($data) { xml_parse($this->parser, $data); } function tag_open($parser, $tag, $attributes) { var_dump($parser, $tag, $attributes); } function cdata($parser, $cdata) { var_dump($parser, $cdata); } function tag_close($parser, $tag) { var_dump($parser, $tag); } } // end of class xml $xml_parser = new xml(); $xml_parser->parse("<A ID='hallo'>PHP</A>"); ?> PHP Documentation Group XML_SET_OBJECT(3)
All times are GMT -4. The time now is 06:02 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy