Solved

want  to  alter  xml

Posted on 2012-03-26
39
723 Views
Last Modified: 2012-05-01
I  have a WAR  file   myapps.war   in  Ubuntu 10  box.

This  WAR file  has a   web.xml  file  at   myapps.war/WEB-INF/web.xml

I  want to  modify  this   xml  file.  In fact, I  want to change few  XML entries basically.

Whats the fastest  way I can do it ?  Please post  steps to check out.
0
Comment
Question by:cofactor
  • 24
  • 13
  • +1
39 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 37767169
You can use sed with -i option.

You have to be more specific what you want to change.

First use sed and if the results are good use -i to commit changes to the file.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37767187
Please, elaborate on your sitauation
Do you mean to do it programmatically?

It is usually not something which people are doing every day - you'll want to eidt and modify it, the speed is normally not an issue, and and probably you'll sometimes need to restart your server.

If you create your web app with IDE - just recreate it, replace the WAR, and it will replace everything on the next starup - thta is probably the most natural way of doing it

Otheriwise that is normal XML file you'll probably use DOM to read and modify it, but I don't think peopel are usually doing it on web.xml
0
 

Author Comment

by:cofactor
ID: 37767481
Simply I  just  want to  change and save web.xml  in a  WAR  file  in Ubuntu 10 Server OS platform.  

whats the quickest solution ?

There is no programming involved here. Looking  for  some  Linux  commands  which could  solve  this problem easily.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37767496
You don't need to change it in the .WAR file if you don't plane to deploy it more than in one place
Just start your server it will expmand .WAR and then change it in web.xml

Or rename your .WAR into. ZIP - use winzip - extract this web.xml and then zip it back and rename into .WAR agaian

Or do all in IDE, modify web.xml and  and then create new .WAR
0
 

Author Comment

by:cofactor
ID: 37767511
Had it been windows , I  would do the following ...

1. Open myapps.war  in  winzip
2.  Right click on web.xml  and open in Notepad
3.  Change web.xml  entries
4.  Save web.xml

Thats it.....very  easy . I  dont need to unzip  the entire war file.


But  this is in windows  .. I  want to do  the same staff  in Ubuntu ...so  my  question is How do I  do it in Ubuntu 10 server OS  platform ?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37767533
well, don't you have some place mapped to windows - I usually deal with Jars and wars this way - copy them to winodws compartible location and do everything form window
There is no doubt that there is unzip and zip progran on Ubuntu and I guess you can even rename it to .jar and then use jar form java tools, but simplest is to copy to windows
0
 

Author Comment

by:cofactor
ID: 37767567
>>>to .jar and then use jar form java tools, but simplest is to copy to windows
Negative.  I  do a FTP upload to  Ubuntu  from  windows.  Not  interested  to download back to windows for  this small  work.

I  wish to  do it  right  on the Ubuntu  platform.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37767572
This is the zip command, and I'm sure you can do it with that:
http://linux.about.com/od/commands/l/blcmdl1_zip.htm

easier to unzip everything and zip it back
then to search for the option which will extract and replace one file

If you do it often with WAR and web.xml specifically - then it is not difficult to write such java program which will extract web.xml for you and another one which will repllace
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37767590
There was a recent question on EE how to extract one specific file from .zip on Linux
Was it your question ?
0
 
LVL 7

Expert Comment

by:gudii9
ID: 37767594
Try XML Copy Editor
Here are interesting tools, links to alter xml
http://xml-copy-editor.sourceforge.net/


http://www.screem.org/
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37767614
do FTP back and forth - it would be much faster than anything else
0
 

Author Comment

by:cofactor
ID: 37767633
>>>There was a recent question on EE how to extract one specific file from .zip on Linux
>>Was it your question.

Yes.  I use that when I  deploy  in exploded  format .  I can change file easily  using  that approach.  That works.

But now I have a .WAR  format  and so I'm in trouble.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37767636
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37767643
>But now I have a .WAR  format  and so I'm in trouble.

But if you rename  or copy .WAR to .ZIP, it should work just as with zip
0
 

Author Comment

by:cofactor
ID: 37767654
>>>do FTP back and forth - it would be much faster than anything else

Negative.   because ..

I  do over SSH .  
size of  WAR  > 100 MB  .  
slow connection.
0
 

Author Comment

by:cofactor
ID: 37767685
>>>But if you rename  or copy .WAR to .ZIP, it should work just as with zip

Are you  trying to say

1.  rename .WAR to .ZIP
2.  extract only web.xml .  dont unzip  entire archive.
3. open vi  editor . change  xml entries.  save changes
4.  put it  back to  .zip  with  overwrite option
5. rename .ZIP  to .WAR


Is  it correct ?  IF so, I  dont know  the command for  step 4.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37767708
Yes, I was saying that.
I don't know the command for step 4 either, but I think I can find it among those zip options.
Let me see.
0
 

Author Comment

by:cofactor
ID: 37767718
>>>You can use sed with -i option.

Can you please  explain how  you  can use sed command to alter web.xml  inside .war  ?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37767735
this recommendation looks suprpaisinly simple:
http://superuser.com/questions/200543/how-to-replace-a-file-in-jar-with-command-line-in-linux

zip -u stuff.jar file.txt

will update file.txt in stuff.zip . Note that for -u file.txt must already exist in the zip file, and will only be overwritten if it's newer than the one in the jar.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:cofactor
ID: 37767736
>>>and  at this question about zip with GUI for Ubuntu:

Ubuntu 10 Server OS  does not have any GUI.  only  command line supported.
0
 

Author Comment

by:cofactor
ID: 37767757
>>>>zip -u stuff.jar file.txt

Don't  you need to  put the path of  the file here ?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37767810
Yes that's how i did it:
I put stuff.zip i  in a folder
then from that folder made directipry say WEB_INF
then placed in that folder modified web.xml
then wen back where i had staff.zip and WEB-INF

and said

zip -u stuff.zip WEB-INF/web.xml

and it upadted
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37767817
I have not Ubuntu but CentOS but I think it should work on onther linux also
try on a small file first
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37767833
so the path should aof course parallel the path within zip:
zip -u stuff.zip WEB-INF/web.xml
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37767935
it even works not only with .zip but with .jar
Unfortunately, .war you still have to rename - it does not know this extension at least on my CENTOS
0
 

Author Comment

by:cofactor
ID: 37769675
>>>zip -u stuff.zip WEB-INF/web.xml

is there any interactive  option here ?  Does "i"  flag  work here. I  just want to  get an overwrite alert .  Yes / No  choice. ..is it possible ?




Also how do I remove a  file from zip ?  I  mistakenly added a file  in archive  in a wrong  path. So I  want to remove that  file also. what command to shoot here ?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37769686
zip -d stuff.zip WEB-INF/web.xml

will remove file

didn't see any interactive option

just type
zip
it will print brief help
0
 

Author Comment

by:cofactor
ID: 37770040
zip -u stuff.zip WEB-INF/web.xml   is not  updating web.xml.

This command does not work.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37770046
I think web.xml in your folder needs to be newer than web.xml in the archive

It updated for me
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37770050
Does it write

"updating ...
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37770063
just tried one more time - everything works fine

but you need to have the ffile in parllaet folder, so that if
you are in some folder and archive ar.zip is in the same folder and in that archive you have
file
a_folder/abc
then you create folder
a_folder
put file
abc into that folder - make sure that abc is modified later than the one in archibve
then go to your first folder

and do

zip -u ar.zip a_folder/abc

and it will write

updating...

and will updtae it
0
 

Author Comment

by:cofactor
ID: 37770067
>>>I think web.xml in your folder needs to be newer than web.xml in the archive

Its a newer file. I  have changed some entries.


I  have  new  web.xml  in deploy folder.
I  have stuff.zip  in deploy folder.

I do this ...

/default/deploy#   zip -u stuff.zip WEB-INF/web.xml

Then again I  do unzip to check  but I see its not updated.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37770074
>I  have  new  web.xml  in deploy folder.
no you need to have new web.xml in WEB-INF folder not in the same folder where you have zip but in parllel to how web.xml sits in the zip archove
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37770077
And it should write "uodating..."
Once you see it writes updating then it will update
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37770080
it cannot be that it wouold updte on centos but not on Ubuntu - I'm sure it is basically the same program
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37770137
This is transcript of my test.
See, first time command
 zip -u forZipTest forZipTest/one/html
did nothing because file "html" in folder  forZipTest/one was of the same date as in archive
then I made  touch forZipTest/one/html to make a newer date
and next time it wrote "updating: forZipTest/one/html (deflated 44%)"
and it indeed updates - I tried with different content of the file also, not just touching

[comp test]$ ls
forZipTest  forZipTest.zip  html  one  ttt1.war  ttt.jar
[comp  test]$ zip -u forZipTest forZipTest/one/html
[comp  test]$ touch forZipTest/one/html
[comp  test]$ zip -u forZipTest forZipTest/one/html
updating: forZipTest/one/html (deflated 44%)
[comp  test]$ zip -Tv forZipTest
Archive:  forZipTest.zip
    testing: forZipTest/one/          OK
    testing: forZipTest/one/html      OK
    testing: forZipTest/one/sample/   OK
    testing: forZipTest/one/sample/one.xx.xml   OK
    testing: forZipTest/one.zip       OK
    testing: forZipTest/two/          OK
    testing: forZipTest/two/html      OK
    testing: forZipTest/two/sample/   OK
    testing: forZipTest/two/sample/two.xx.xml   OK
    testing: forZipTest/two.zip       OK
    testing: forZipTest/              OK
    testing: html                     OK
    testing: one/html                 OK
No errors detected in compressed data of forZipTest.zip.
test of forZipTest.zip OK
[comp  test]$ nedit log.txt

Open in new window

0
 

Author Comment

by:cofactor
ID: 37770713
>>>zip -u

I'm not clear about source and destination . I have stuff.zip  and  new  web.xml in the same folder.  ..so can I do this ?

zip -u   stuff.zip   web.xml   stuff.zip/WEB-INF/web.xml


>>>>And it should write "uodating..."

It does not .
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37771046
No yoiu cannot. You need to create the path similar to the one inside your zip.
So if inside your stuff.zip you have web.xml within the folder WEB-INF, so you need to create the folder WEB-INF in your current directory and put yourneew variant of web.xml into that folder, then you go back to your prompt in current directory. Then from this prompt the real new version of filke will be accessible in the same path as it is sitting within the zip. And from this point you say
Zip -u stuff.zip WEB-INF/web.xml
Then it would write "updating..." And would update

I posted thge transcript of real example above - look at that and see how it works.
You cannot hjave it in the same folder as when youi specify the file oin the command itt should simultaneoiusly indicate the locatin of the file in zip and in the folder structure
0
 
LVL 47

Accepted Solution

by:
for_yan earned 250 total points
ID: 37771196
You can alos think about it this way.

Suppose you had a .zip file in some folder.
Then you unzipped this zip file in this folder - so it creted for you the folder structure starting from this filder. Then someone , or you yourslef, modified one of the files within the folder structure where it was expanded. Then sitting in the same folder where you initially unzipped your zip file and still having your source zip file in that same folder, right here you are using zip -u command and you need to specify the path to that individual file which was modified on this command line. That path would be the same in your real unzipped directory structue and withinn the zip, so you specify this path as the second argument of your command (not counting -u which is not the argument but a switch).
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now