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::Smart::Tutorial(3pm)				User Contributed Perl Documentation				 XML::Smart::Tutorial(3pm)

NAME
XML::Smart::Tutorial - Tutorial and examples for XML::Smart. SYNOPSIS
This document is a tutorial for XML::Smart and shows some examples of usual things. Working with contents: In XML::Smart the key CONTENT is reserved and shouldn't be used directly, since XML::Smart will deal with the convertion of arguments to node contents, including multiple node contents autimatically. What happens when you set a value: $xml->{root}{foo} = 'simple value' ; Here foo will be a normal argument/attribute value, and will generate this XML data: <root foo="simple value"/> But if you insert some tag or lines in the values by default XML::Smart will convert it to a node content: $xml->{root}{foo} = "line0 lien1 line2 " ; And will generate that XML data: <root> <foo>line0 lien1 line2 </foo> </root> But what you can do if you want to force some type, let's say, have a node content with a simple value: $xml->{root}{foo} = 'simple value' ; $xml->{root}{foo}->set_node(1) ; And will generate that XML data: <root> <foo>simple value</foo> </root> Multiple contents: When you have interpolated content/data you need to work in a different. Let's say that you load this XML data: <root> content0 <tag1 arg="1"/> content1 </root> If you access directly the root key as string you will get all the content parts grouped. So, this code: my $xml = new XML::Smart(q` <root> content0 <tag1 arg="1"/> content1 </root> `,'smart') ; print "#$xml->{root}#" ; Will print that: # content0 content1 # To access each part of the content independently you should use an array that receive the method content(): my @content = $xml->{root}->content ; print "#$content[0]# " ; And this will print that: # content0 # Now to set the multiple content values you should use the method content() with 2 arguments: $xml->{root}->content(0,'new content') ; And now the XML data produced will be: <root>new content<tag1 arg="1"/> content1 </root> If you use the method content() with only one argument it will remove all the multiple contents and will set the new value in the place of the 1st content. Setting the XML Parser. By defaul XML::Smart will use XML::Parser or XML::Smart::Parser (in this order of preference) to load a XML data. To force or define by your self the parser you can use the 2nd argument option when creating a XML::Smart object: my $xml = new XML::Smart( 'some.xml' , 'XML::Parser' ) ; ## and my $xml = new XML::Smart( 'some.xml' , 'XML::Smart::Parser' ) ; XML::Smart also has an extra parser, XML::Smart::HTMLParser, that can be used to load HTML as XML, or to load wild XML data: my $xml = new XML::Smart( 'some.xml' , 'XML::Smart::HTMLParser' ) ; Aliases for the parser options: SMART|REGEXP => XML::Smart::Parser HTML => XML::Smart::HTMLParser So, you can use as: my $xml = new XML::Smart( 'some.xml' , 'smart' ) ; my $xml = new XML::Smart( 'some.xml' , 'html' ) ; Customizing the Parser. You can customize the way that the parser will treat the XML data: Forcing nodes/tags and arguments/attributes to lowercase or upercase: ## For lower case: my $xml = new XML::Smart( 'some.xml' , lowtag => 1 , lowarg => 1 , ) ; ## For uper case: my $xml = new XML::Smart( 'some.xml' , upertag => 1 , uperarg => 1 , ) ; Loading arguments without values (flags) as a TRUE boolean: ** Note, this option will work only when the XML is parsed by XML::Smart::HTMLParser, since only it accept arguments without values! my $xml = new XML::Smart( '<root><foo arg1="" flag></root>' , 'XML::Smart::HTMLParser' , arg_single => 1 , ) ; Here's the tree of the example above: 'root' => { 'foo' => { 'flag' => 1, 'arg1' => '' }, }, Customizing the parse events: XML::Smart can redirect the parsing process to personalized functions: my $xml = XML::Smart->new( 'some.xml' , on_start => &on_start , on_char => &on_char , on_end => &on_end , ) ; sub on_start { my ( $tag , $pointer , $pointer_back ) = @_ ; $pointer->{$tag}{type_user} = 1 if $tag =~ /(?:name|age)/ ; } sub on_char { my ( $tag , $pointer , $pointer_back , $content) = @_ ; $$content =~ s/s+/ /gs ; } sub on_end { my ( $tag , $pointer , $pointer_back ) = @_ ; $pointer->{$tag}{type_extra} = 1 if $tag =~ /(?:more|tel|address)/ ; } AUTHOR
Graciliano M. P. <gm@virtuasites.com.br> I will appreciate any type of feedback (include your opinions and/or suggestions). ;-P Enjoy and thanks for who are enjoying this tool and have sent e-mails! ;-P ePod This document was written in ePod (easy-POD), than converted to POD, and from here you know the way. perl v5.10.1 2004-12-08 XML::Smart::Tutorial(3pm)
All times are GMT -4. The time now is 06:21 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy