insert a block to xml file java -
<?xml version="1.0" encoding="utf-8"?> <dbvisualizer> <databases> <database id="1"> <alias>mymssqlserver</alias> <url /> <driver>sql server (jtds)</driver> <userid>db_monitor</userid> <profile>auto</profile> <type>sqlserver</type> <password>abcdefghsifjsdsdkjsd</password> <serverinfoformat>1</serverinfoformat> <autodetecttype>true</autodetecttype> <properties> <property key="dbvis.connectionmodemigrated">true</property> </properties> <urlformat>0</urlformat> <urlvariables> <driver> sql server (jtds) <urlvariable urlvariablename="server">192.168.1.1</urlvariable> <urlvariable urlvariablename="port">1433</urlvariable> <urlvariable urlvariablename="database">abcdefg</urlvariable> </driver> </urlvariables> <sshsettings> <sshenabled>false</sshenabled> <sshhost /> <sshport>22</sshport> <sshuserid /> <sshpassword /> <sshprivatekeyfile /> <sshpassphrase /> </sshsettings> </database> </dbvisualizer>
and have provision process generated new server me sqlserver on , need add there properties xml file, need duplicate section of element "database" change few properties , push right after database element exist, created template section in external file changing ever need how can append entire section file, aware can 1 element @ time thats lot of element want push entire section after 1 have:
<database id="idnumber"> <alias>machiname</alias> <url /> <driver>sql server (jtds)</driver> <userid>db_monitor</userid> <profile>auto</profile> <type>sqlserver</type> <password>abcdefghsifjsdsdkjsd==</password> <serverinfoformat>1</serverinfoformat> <autodetecttype>true</autodetecttype> <properties> <property key="dbvis.connectionmodemigrated">true</property> </properties> <urlformat>0</urlformat> <urlvariables> <driver> sql server (jtds) <urlvariable urlvariablename="server">machineip</urlvariable> <urlvariable urlvariablename="port">1433</urlvariable> <urlvariable urlvariablename="database">abcdefg</urlvariable> </driver> </urlvariables> <sshsettings> <sshenabled>false</sshenabled> <sshhost /> <sshport>22</sshport> <sshuserid /> <sshpassword /> <sshprivatekeyfile /> <sshpassphrase /> </sshsettings> </database>
i assume have main xml document inside object mydocument
instance of class org.w3c.dom.document
, have current template instance inside object newdatabase
instance of class org.w3c.dom.node
.
now have retrieve <databases />
node mydocument
, append newdatabase
node it
nodelist nodes = mydocument.getelementsbytagname("databases"); nodes.item(0).appendchild(newdatabase);
the appendchild() method adds node after last child node of specified element node.
-- edit: read template file , obtain node can do
fileinputstream fileinputstream = new fileinputstream(template_file_path); document document = documentbuilderfactory.newinstance().newdocumentbuilder().parse(fileinputstream); node newdatabase = document.getdocumentelement();
-- second edit: before appending newdatabase
node mydocument
must duplicate node , transfer ownership of node destination document line of code:
node newnode = mydocument.importnode(newdatabase, true);
then can append newnode
mydocument
using previous code.
Comments
Post a Comment