Modify multiple lines of an XML file using command line
Use a proper xmltool, in shell, xmlstarlet is a good one :
xmlstarlet edit -L -u "//Model500[1]" -v "AAA
BBB
CCC" file.xml
xmlstarlet edit -L -u "//Model500[2]" -v "111
333
555" file.xml
cat file.xml
The expression //Model500[2]
is a Xpath expression
NOTE ¹
you need to enclose your xml with a tag like :
<root>
...
</root>
to be XML valid. I hope you cutted the XML file for the purpose of your question.
NOTE ²
You can also use python, perl, ruby etc... and a proper xml lib.
In perl :
#!/usr/bin/perl
use strict;
use warnings;
use XML::Simple;
my $xml_file = 'file.xml';
my $xml = XMLin(
$xml_file,
KeepRoot => 1,
ForceArray => 1,
);
$xml->{root}->[0]->{Model500}->[0] = "AAA\nBBB\nCCC\n";
$xml->{root}->[0]->{Model500}->[1] = "111\n222\n333\n";
XMLout(
$xml,
KeepRoot => 1,
NoAttr => 1,
OutputFile => $xml_file,
);
Related videos on Youtube
Johnny
Updated on September 18, 2022Comments
-
Johnny over 1 year
I have a users.php where admin can see all registered users information and he is able to change their levels with a form that goes to edit_level.php.
It's not working properly, when I change the level of a user, it is affecting the wrong user.
This is my form in users.php (I didn’t include all the user info , just the level part)
$sql = "SELECT * FROM users ORDER BY username ASC"; $result = mysqli_query($conn,$sql) or die(mysqli_error()); if(mysqli_num_rows($result) > 0){ while ($row = mysqli_fetch_assoc($result)){ $level = $row['level']; <form method='post' action='edit_level.php?ed=$id'> <input type='hidden' name='id' value='$row[id]'> <input type='text' name='level' value='$row[level]'> <input type='submit' name='submit' value='Change Level'> }
This is edit_level.php
if(isset($_POST['submit'])){ $ed_id = $_GET['ed']; $level = $_POST['level']; $sql = "UPDATE users SET level ='$level' WHERE id='$ed_id'"; if(mysqli_query($conn,$sql)){ echo "<p>User Level has been sucessfully udpated! <a href='users.php'>Click here to return to User List.</a>"; } else{ echo "<p><b>ERROR:User level has not been updated!"; } }
Edit:
I actually just copied the same format of a delete function I have, but the delete was a link not a form.
<a href='deleteaccount.php?del=$id'>Delete</a> and then in the php: $del_id = $_GET['del']; $sql = "DELETE FROM users WHERE id='$del_id'";
Not sure how to do the same but with a form. I need a form because I want to be able to type a new value.
-
Tim3880 almost 9 yearsYour post form stores the id as "id" but your script reads "ed", is that the problem?
-
AnkiiG almost 9 years@Johnny : You have created form in while loop. So if there are 10 users then 10 forms will be created and all the hidden fields will have same name. This will not give you correct post values.
-
Johnny almost 9 yearsYes for each user there is a Submit button next to it. How should I do it?
-
Johnny almost 9 yearsI had to use a form in a loop because I want to read the level from user table and be able to edit it and submit the changes. Not sure how to fix this.
-
AnkiiG almost 9 yearsYou should create one form and call ajax to update
-
Johnny almost 9 yearsHmm I'm not good with ajax
-
Johnny almost 9 yearsNo other way to do it? Where should my form be placed?
-
Lupin almost 9 yearsYou are editing a record according to this:
action='edit_level.php?ed=$id'
, where do you take the$id
from? -
Johnny almost 9 yearsI don't know, i actually just copied the same format of a delete function I have, but the delete was a link not a form. <a href='deleteaccount.php?del=$id'>Delete</a> and then in the php: $del_id = $_GET['del']; $sql = "DELETE FROM users WHERE id='$del_id'"; Not sure how to do the same but with a form. I need a form cuz I want to be able to type a new value.
-
hakkikonu almost 9 yearsecho your post values on edit_level.php? What you're seeing?
-
Johnny almost 9 yearsI did echo $level = $_POST['level']; Im seeing Registered for everyone, even for Administrator. I have 2 possible values for level in user table, Registered and Adminsitrator, default is Registered. Here is the DB line-->
level
ENUM('Registered','Administrator') NOT NULL DEFAULT 'Registered', -
Johnny almost 9 yearsCheck this postimg.org/image/tbt3ti331, thats how my users.php look like
-
Jeff Schaller over 7 yearsbash has a lot of features, but it's not a text editor
-
Ipor Sircer over 7 yearswhat's the rule?
-
keyboard_solo over 7 yearsWell I do understand that the bash is not supposed to be used for extensive text editing, but I thought one can use 'sed' for that purpose?
-
Jeff Schaller over 7 yearsI got confused when you said "any bash method" -- bash != sed
-
Jesusaur over 7 yearsAre the replacements based on rules, or is the end result not related to the initial values?
-
keyboard_solo over 7 yearsThe end results are not related to the initial values. This code is just a snippet that I intend to integrate into a bigger set. For this instance, I would replace the ABC, BCD, etc with the user input. I am just looking for ways to easily modify the XML file where a certain variable repeats. It turns out 'sed' isn't the best way to modify XML files.
-
CJ Dana over 7 yearsUse any text editor in bash.(vi, vim, nano)
-
-
Johnny almost 9 yearsI am a bit confused by what you said, I don't really understand what changes to make.
-
Johnny almost 9 yearswhen im clicking any button, im getting edit_level.php?ed=2 ed is always equal 2
-
Johnny almost 9 yearsanother thing, the only changes that happen is when I change the value of username c and press submit, it changes the value of username a (usernames according to the picture)
-
Johnny almost 9 yearsOnly the first row is getting updated, and its value only changes when the last row Submit button is pressed
-
Johnny almost 9 yearsCan anyone help please?
-
keyboard_solo over 7 yearsWoah I have not heard of this tool but the format on this seems much more intuitive. Thanks. I will give this one a try.