Insert a line in XML File

February 1, 2010 at 03:31:31
Specs: Windows XP
Hi All,
I have an xml file which contains lot of data. I
want to search for a particular string and then
would like to insert a line after the line in which
string was available.

How can I do this from batch file. Please help
me in this.

Sample Contents of XML File:
<?xml version="1.0" encoding="UTF-8"?>

<!-- $Id: jboss-service.xml 38992 2005-12-13
15:29:35Z jerrygauth $ -->

<!--
=================================
=================================
=== -->
<!-- JBoss Server Configuration
-->
<!--
=================================
=================================
=== -->

<server>

<!-- Load all jars from the
JBOSS_DIST/server/<config>/lib directory.
This
can be restricted to specific jars by
specifying them in the archives
attribute.
-->
<classpath
codebase="${jboss.server.lib.url:lib}"
archives="*"/>

<!--
=================================
=================================
== -->
<!-- JSR-77 Single JBoss Server
Management Domain -->
<!--
=================================
=================================
== -->
<mbean
code="org.jboss.management.j2ee.LocalJBos
sServerDomain"

name="jboss.management.local:j2eeType=J2
EEDomain,name=Manager">
<attribute
name="MainDeployer">jboss.system:service=
MainDeployer</attribute>
<attribute
name="SARDeployer">jboss.system:service=
ServiceDeployer</attribute>
<attribute
name="EARDeployer">jboss.j2ee:service=EA
RDeployer</attribute>
<attribute
name="EJBDeployer">jboss.ejb:service=EJBD
eployer</attribute>
<attribute
name="RARDeployer">jboss.jca:service=RAR
Deployer</attribute>
.............


Would like to search for "classpath codebase"
and insert a line after that as:

<classpath
codebase="${jboss.home}\shared\lib"
archives="*"/>


See More: Insert a line in XML File

Report •

#1
February 1, 2010 at 05:29:15
@echo off > newfile & setLocal enableDELAYedeXpansion

for /f "tokens=1 delims=[]" %%a in ('find /n "classpath codebase" ^< my.xml') do (
set Q=%%a
)

for /f "tokens=1* delims=[]" %%a in ('find /n /v "" ^< my.xml') do (
>> newfile echo.%%b
if %%a equ !Q! (
>> newfile echo my new string
)
)


=====================================
Helping others achieve escape felicity

M2


Report •

#2
February 1, 2010 at 21:23:17
Hi M2,
Thanks for the immediate reply.
I tried the suggested script. But the script is omitting few
symbols in XML file which are important for the XML format
while writing to new file.

Content of new file is:
<?xml version="1.0" encoding="UTF-8"?>

< jboss-service.xml 38992 2005-12-13 15:29:35Z jerrygauth $
-->

<--
============================================
========================= -->
<-- JBoss Server Configuration --
>
<--
============================================
========================= -->

<server>

<-- Load all jars from the JBOSS_DIST/server/<config>/lib
directory. This
can be restricted to specific jars by specifying them in the
archives
attribute.
-->
<classpath codebase="${jboss.server.lib.url:lib}"
archives="*"/>
my new string

where as each line should have <!-- which is in the original file.
Please see my above message for the original content of file



Report •

#3
February 1, 2010 at 22:32:30
the enabledelayedexpansion zaps the ! everytime.
might work if you remove that clause so line 1 is:
@echo off>newfile & setlocal
then replace the exclams around Q with percents
(!Q! --> %Q%)
if %%a equ %Q% (

since his construction doesn't require enabledelayed i don't think. Q has it's value prior to entering the loop, so it's ok.
warning flag: if XML, no XCLM! (bad pun, sorry)


Report •

Related Solutions

#4
February 1, 2010 at 22:50:23
I think it's a pretty good pun.

Try this:

================================

@echo off > newfile 

for /f "tokens=1 delims=[]" %%a in ('find /n "classpath codebase" ^< my.xml') do (
set Q=%%a
)

for /f "tokens=1* delims=[]" %%a in ('find /n /v "" ^< my.xml') do (
>> newfile echo.%%b
if %%a equ %Q% (
>> newfile echo my new string
)
)


=====================================
Helping others achieve escape felicity

M2


Report •

#5
February 1, 2010 at 23:38:57
ha!
might want to forwarn op, heads up on the <> also:
<classpath
codebase="${jboss.home}\shared\lib"
archives="*"/>

needs the >< escaped so your echo looks like:

>> newfile echo ^<classpath
codebase="${jboss.home}\shared\lib"
archives="*"/^>


Report •

#6
February 2, 2010 at 00:16:42
Thanks alot to nBrane and M2.

My problem is really solved.


Report •

Ask Question