Modify multiple lines of an XML file using command line

263

Use a proper tool, in shell, 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 , , etc... and a proper xml lib.

In :

#!/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,
);
Share:
263

Related videos on Youtube

Johnny
Author by

Johnny

Updated on September 18, 2022

Comments

  • Johnny
    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
      Tim3880 almost 9 years
      Your post form stores the id as "id" but your script reads "ed", is that the problem?
    • AnkiiG
      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
      Johnny almost 9 years
      Yes for each user there is a Submit button next to it. How should I do it?
    • Johnny
      Johnny almost 9 years
      I 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
      AnkiiG almost 9 years
      You should create one form and call ajax to update
    • Johnny
      Johnny almost 9 years
      Hmm I'm not good with ajax
    • Johnny
      Johnny almost 9 years
      No other way to do it? Where should my form be placed?
    • Lupin
      Lupin almost 9 years
      You are editing a record according to this: action='edit_level.php?ed=$id', where do you take the $id from?
    • Johnny
      Johnny almost 9 years
      I 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
      hakkikonu almost 9 years
      echo your post values on edit_level.php? What you're seeing?
    • Johnny
      Johnny almost 9 years
      I 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
      Johnny almost 9 years
      Check this postimg.org/image/tbt3ti331, thats how my users.php look like
    • Jeff Schaller
      Jeff Schaller over 7 years
      bash has a lot of features, but it's not a text editor
    • Ipor Sircer
      Ipor Sircer over 7 years
      what's the rule?
    • keyboard_solo
      keyboard_solo over 7 years
      Well 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
      Jeff Schaller over 7 years
      I got confused when you said "any bash method" -- bash != sed
    • Jesusaur
      Jesusaur over 7 years
      Are the replacements based on rules, or is the end result not related to the initial values?
    • keyboard_solo
      keyboard_solo over 7 years
      The 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
      CJ Dana over 7 years
      Use any text editor in bash.(vi, vim, nano)
  • Johnny
    Johnny almost 9 years
    I am a bit confused by what you said, I don't really understand what changes to make.
  • Johnny
    Johnny almost 9 years
    when im clicking any button, im getting edit_level.php?ed=2 ed is always equal 2
  • Johnny
    Johnny almost 9 years
    another 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
    Johnny almost 9 years
    Only the first row is getting updated, and its value only changes when the last row Submit button is pressed
  • Johnny
    Johnny almost 9 years
    Can anyone help please?
  • keyboard_solo
    keyboard_solo over 7 years
    Woah I have not heard of this tool but the format on this seems much more intuitive. Thanks. I will give this one a try.