LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-15-2012, 01:13 PM   #1
gautamshrm3
LQ Newbie
 
Registered: Dec 2012
Posts: 2

Rep: Reputation: Disabled
xml file editing


Hi i am new Linux script
I have a xml and i want to edit it pls help me

before editing ::

<root>
<name att="abc">
<address att="xyz"/>
<address att="xyz"/>
</name>
</root>

after editing

<root>
<name att="abc">
<address att="xyz"/>
<address att="xyz"/>
</name>
<name att="abc"></name>
</root>

how can i do this pls help me and suggest me can i use xml parser(how) or any other way to implement this.
 
Old 12-15-2012, 01:32 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,991

Rep: Reputation: 4327Reputation: 4327Reputation: 4327Reputation: 4327Reputation: 4327Reputation: 4327Reputation: 4327Reputation: 4327Reputation: 4327Reputation: 4327Reputation: 4327
Quote:
Originally Posted by gautamshrm3 View Post
Hi i am new Linux script
I have a xml and i want to edit it pls help me

before editing ::

<root>
<name att="abc">
<address att="xyz"/>
<address att="xyz"/>
</name>
</root>

after editing

<root>
<name att="abc">
<address att="xyz"/>
<address att="xyz"/>
</name>
<name att="abc"></name>
</root>

how can i do this pls help me and suggest me can i use xml parser(how) or any other way to implement this.
Please spell out your words. And you've asked a question, without providing details/context. What language do you want this program to be in? Perl has excellent XML parsing libraries, and there are ways to do this in bash, python, ruby, and pretty much any programming language.

Also, is all you want to modify the 'name' field? How big is/are the file(s)? How many? And this sounds very much like a homework question...can you post what you've written/tried so far?? We are happy to HELP you, but will not write it for you. There are many bash scripting examples (if that's what you want), that you can find easily with Google...have you looked?
http://stackoverflow.com/questions/1...-from-xml-file
http://stackoverflow.com/questions/4...ng-shellscript
 
Old 12-19-2012, 07:50 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.


As the links TBOne gave above mention, there are several xml parsing tools available to use. I've recently been enjoying learning how to use xmlstarlet and xpath, and here's what I came up with to insert a new name element into your example file:

(note that the "+" is just my $PS2 prompt, indicating a multi-line command)

Code:
$ xmlstarlet ed -O -a '//name[@att="abc" and last()]' -t elem -n name -v '' \
+ -a '//name[last() and not(@att)]' -t attr -n att -v abc file.xml
<?xml version="1.0"?>
<root>
  <name att="abc">
    <address att="xyz"/>
    <address att="xyz"/>
  </name>
  <name att="abc"/>
</root>
Unfortunately I can't seem to both add an element and set an attribute for it in the same step. I'm not sure it's possible with this tool. And of course it inserted the single-tag "<../>" format because I didn't supply it with a value. Give the -v option a text string, or add a sub-element, and it will output a full tag.

Code:
$ xmlstarlet ed \
+ -a '//name[@att="abc" and last()]' -t elem -n name -v '' \
+ -a '//name[last() and not(@att)]' -t attr -n att -v abc \
+ -s '//name[@att="abc" and last()]' -t elem -n address -v '' \
+ -a '//name[last()]/address[not(@att)]' -t attr -n att -v xyz file.xml
<?xml version="1.0"?>
<root>
  <name att="abc">
    <address att="xyz"/>
    <address att="xyz"/>
    <address/>
  </name>
  <name att="abc">
    <address att="xyz"/>
  </name>
</root>
I'm still just a beginner, though, so there are likely ways to shorten up the command.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to add xml-stylesheet tag in a XML File using libxml2 ? peacemission Programming 6 05-26-2012 02:20 AM
[SOLVED] XML editing: adding elements to xml lists alexwely Programming 7 04-20-2012 12:45 PM
Editing XML syedfh Linux - Newbie 5 01-10-2010 03:48 PM
Editing 'google.xml' Chriscrof Linux - Newbie 3 09-19-2008 02:27 AM
PHP5 SimpleXML (problem editing XML) carlosruiz Programming 1 06-23-2005 02:16 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration