Sponsored Content
Top Forums Shell Programming and Scripting Grab XML attributes from file with bash script Post 303045636 by joddclaude on Sunday 5th of April 2020 08:15:48 AM
Old 04-05-2020
Grab XML attributes from file with bash script

Hi,

I need to grab attributes from the XML file with pure bash script.

So I have the following XML file with a root element `Group` and lots of `Person` elements, every of them has `id` and `username` attributes. `id` is unique value for each element:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<Group id="RW_8987"
       dept_id="D_12"
       main="false">

    <Person id="P_0001"
            email="email0001@example.com"
            username="person_0001"
            password="pass_0001"/>

    <Person id="P_0002"
            email="email0002@example.com"
            username="person_0002"
            password="pass_0002"/>

    <!--  ...  -->
</Group>

And I need to use bash script to extract the `id` and `username` attributes into some key-value structure:

Code:
P_0001=person_0001
P_0002=person_0002

Checked other related answers, but most of them suggest to use some XML parsers like xmllint. But unfortunately I do not have them on the target machine.

Can you kindly suggest what how I can achieve this. Thanks in advance.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove xml namespace from xml file using shell script?

I have an xml file: <AutoData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Table1> <Data1 10 </Data1> <Data2 20 </Data2> <Data3 40 </Data3> <Table1> </AutoData> and I have to remove the portion xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" only. I tried using sed... (10 Replies)
Discussion started by: Gary1978
10 Replies

2. Windows & DOS: Issues & Discussions

DOS script to grab the first file in a dir and rename it

:confused: Hello, Is there any way to use the dir command / some DOS Script to select only first file of similar pattern of files in a direcotory and rename it for example, one directory has 5 files abc_1005.txt abc_5256.txt abc_2001.txt abc_2003.txt abc_3006.txt by use script I would... (2 Replies)
Discussion started by: raghav525
2 Replies

3. Shell Programming and Scripting

reading xml attributes with shell script

Hi, Iam new to shell scripting.I have below urgent requirement I want to read attributes (transaction,documentmode) in xml tag with shell scripting and create a filename with these attribues Xml : <PURCHASE_10 partner="food" version="1.50" timestamp="2009-03-10T09:56:55"... (3 Replies)
Discussion started by: swetha123
3 Replies

4. Shell Programming and Scripting

Need to Parse XML from bash script

I am completely new to bash scripting and now need to write a bash script that would parse a XML file and take out values from specific tags. I tried using xsltproc, xml_grep commands. But the issue is that the XML i am trying to parse is not UTF 8. so those commands are unable to parse my XML's... (4 Replies)
Discussion started by: shivashankar.g
4 Replies

5. Shell Programming and Scripting

Rewriting file paths in XML file within bash script

Hi guys, I'm working on a large set of scripts to move files around several servers and manipulate them for our staff. Basically we're shooting things, the videos hit a server and then need organised due to the language they've been shot in. Our XML (designed for Apple's Final Cut Pro) is right... (6 Replies)
Discussion started by: omfgbunnies
6 Replies

6. Shell Programming and Scripting

bash extract all occurences delimited from <name> and </name> tags from an xml file

I need to extract all text delimited from <name> and </name> tags from an xml file, but not only first occurence. I need to extract all occurences. I've tried with this command: awk -F"<name>|</name>" 'NF>2{print $2}' but it give only first occurence. How can i modify it? (18 Replies)
Discussion started by: ingalex
18 Replies

7. Shell Programming and Scripting

XML- Sed || Awk Bash script... Help!

Hi ! I'm working into my first bash script to make some xml modification and it's going to make me crazy lol .. so I decide to try into this forum to take some ideas from people that really know about this! This is my situation I've and xml file with a lots of positional values with another tags... (9 Replies)
Discussion started by: juampal
9 Replies

8. Shell Programming and Scripting

How to grab data from xml block?

I tried searching the forums, but couldn't find anything relevant to my question. I have an xml file like the following: <topLevel numberBlock="BLOCK1"> <item="content1" title="Content 1"> <RefPath="path/to/file1.txt /> </item> <item"content2" title="Content 2" >... (4 Replies)
Discussion started by: jl487
4 Replies

9. Shell Programming and Scripting

BASH script to parse XML and generate CSV

Hi All, Hope all you are doing good! Need your help. I have an XML file which needs to be converted CSV file. I am not an expert of awk/sed so your help is highly appreciated!! XML file looks like this: <l:event dateTime="2013-03-13 07:15:54.713" layerName="OSB" processName="ABC"... (2 Replies)
Discussion started by: bhaskar_m
2 Replies

10. UNIX for Beginners Questions & Answers

How to insert subnode in xml file using xmlstarlet or any other bash command?

I have multiple xml files where i want to update a subnode if the subnode project points to different project or insert a subnode if it doesn't exist using a xmlstarlet or any other command that can be used in a bash script. I have been able to update the subnode project if it doesn't point to... (1 Reply)
Discussion started by: Sekhar419
1 Replies
XML::PatAct::ToObjects(3pm)				User Contributed Perl Documentation			       XML::PatAct::ToObjects(3pm)

NAME
XML::PatAct::ToObjects - An action module for creating Perl objects SYNOPSIS
use XML::PatAct::ToObjects; my $patterns = [ PATTERN => [ OPTIONS ], PATTERN => "PERL-CODE", ... ]; my $matcher = XML::PatAct::ToObjects->new( Patterns => $patterns, Matcher => $matcher, CopyId => 1, CopyAttributes => 1 ); DESCRIPTION
XML::PatAct::ToObjects is a PerlSAX handler for applying pattern-action lists to XML parses or trees. XML::PatAct::ToObjects creates Perl objects of the types and contents of the action items you define. New XML::PatAct::ToObject instances are creating by calling `new()'. Parameters can be passed as a list of key, value pairs or a hash. `new()' requires the Patterns and Matcher parameters, the rest are optional: Patterns The pattern-action list to apply. Matcher An instance of the pattern or query matching module. CopyId Causes the `ID' attribute, if any, in a source XML element to be copied to an `ID' attribute in newly created objects. Note that IDs may be lost of no pattern matches that element or an object is not created ("-make") for that element. CopyAttributes Causes all attributes of the element to be copied to the newly created objects. Each action can either be a list of options defined below or a string containing a fragment of Perl code. If the action is a string of Perl code then simple then some simple substitutions are made as described further below. Options that can be used in an action item containing an option-list: -holder Ignore this element, but continue processing it's children (compare to -ignore). "-pcdata" may be used with this option. -ignore Ignore (discard) this element and it's children (compare to -holder). -pcdata Character data in this element should be copied to the "Contents" field. -make PACKAGE Create an object blessed into PACKAGE, and continue processing this element and it's children. PACKAGE may be the type `"HASH"' to simply create an anonyous hash. -args ARGUMENTS Use ARGUMENTS in creating the object specified by -make. This is commonly used to copy element attributes into fields in the newly created object. For example: -make => 'HASH', -args => 'URL => %{href}' would copy the `"href"' attribute in an element to the `"URL"' field of the newly created hash. -field FIELD Store this element, object, or children of this element in the parent object's field named by FIELD. -push-field FIELD Similar to -field, except that FIELD is an array and the contents are pushed onto that array. -value VALUE Use VALUE as a literal value to store in FIELD, otherwise ignoring this element and it's children. Only valid with -field or -push-field. `"%{ATTRIBUTE}"' notation can be used to substitute the value of an attribute into the literal value. -as-string Convert the contents of this element to a string (as in "XML::Grove::AsString") and store in FIELD. Only valid with -field or -push-field. -grove Copy this element to FIELD without further processing. The element can then be processed later as the Perl objects are manipulated. Only valid with -field or -push-field. If ToObjects is used with PerlSAX, this will use XML::Grove::Builder to build the grove element. -grove-contents Used with -make, -grove-contents creates an object but then takes all of the content of that element and stores it in Contents. If an action item is a string, that string is treated as a fragment of Perl code. The following simple substitutions are performed on the fragment to provide easy access to the information being converted: @ELEM@ The object that caused this action to be called. If ToObjects is used with PerlSAX this will be a hash with the element name and attributes, with XML::Grove this will be the element object, with Data::Grove it will be the matching object, and with XML::DOM it will be an XML::DOM::Element. EXAMPLE
The example pattern-action list below will convert the following XML representing a Database schema: <schema> <table> <name>MyTable</name> <summary>A short summary</summary> <description>A long description that may contain a subset of HTML</description> <column> <name>MyColumn1</name> <summary>A short summary</summary> <description>A long description</description> <unique/> <non-null/> <default>42</default> </column> </table> </schema> into Perl objects looking like: [ { Name => "MyTable", Summary => "A short summary", Description => $grove_object, Columns => [ { Name => "MyColumn1", Summary => "A short summary", Description => $grove_object, Unique => 1, NonNull => 1, Default => 42 } ] } ] Here is a Perl script and pattern-action list that will perform the conversion using the simple name matching pattern module XML::PatAct::MatchName. The script accepts a Schema XML file as an argument ($ARGV[0]) to the script. This script creates a grove as one of it's objects, so it requires the XML::Grove module. use XML::Parser::PerlSAX; use XML::PatAct::MatchName; use XML::PatAct::ToObjects; my $patterns = [ 'schema' => [ qw{ -holder } ], 'table' => [ qw{ -make Schema::Table } ], 'name' => [ qw{ -field Name -as-string } ], 'summary' => [ qw{ -field Summary -as-string } ], 'description' => [ qw{ -field Description -grove } ], 'column' => [ qw{ -make Schema::Column -push-field Columns } ], 'unique' => [ qw{ -field Unique -value 1 } ], 'non-null' => [ qw{ -field NonNull -value 1 } ], 'default' => [ qw{ -field Default -as-string } ], ]; my $matcher = XML::PatAct::MatchName->new( Patterns => $patterns ); my $handler = XML::PatAct::ToObjects->new( Patterns => $patterns, Matcher => $matcher); my $parser = XML::Parser::PerlSAX->new( Handler => $handler ); my $schema = $parser->parse(Source => { SystemId => $ARGV[0] } ); TODO
o It'd be nice if patterns could be applied even in -as-string and -grove. o Implement Perl code actions. o -as-xml to write XML into the field. AUTHOR
Ken MacLeod, ken@bitsko.slc.ut.us SEE ALSO
perl(1), Data::Grove(3) ``Using PatAct Modules'' and ``Creating PatAct Modules'' in libxml-perl. perl v5.10.1 2010-01-28 XML::PatAct::ToObjects(3pm)
All times are GMT -4. The time now is 04:04 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy