How to update/change the value of elements in an xml file?

Hi Everyone,
Could any one of u tell me how to update the value of elements in an XML file, using java? The reason is i want to use an XML file as a data source (i.e. more or less like a database), without using any RDBMS, for simple applications such as to read a record and update the record. By the way, my XML file will have only one record, such as the current weather information, with fields such as temperature, humdity etc. for 1 city only.
Thanks in advance.

Here is a solution how to check a particular value or element name in an xml and update the changes e to an xml.
Sample.xml
<URLConstructor>
<application name="cp_outage">
<resource>hello</resource>
<value>val</value>
</application>
<application name="cp_outage">
<resource>hello</resource>
<value>val</value>
</application>
</URLConstructor>
XMLWriter.java
package com;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.DOMBuilder;
import org.jdom.output.XMLOutputter;
// used for printing
import org.apache.xml.serialize.XMLSerializer;
import org.jdom.output.XMLOutputter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
class XMLWriter{
public void update(File fileName)
try {
          DOMBuilder domBuilder=new DOMBuilder();
          Document doc=domBuilder.build(fileName);
          Element element=doc.getRootElement();
          getChildren(element);
          writeToXML(doc,fileName);
          getChildren(element);
} catch (Exception e) {
e.printStackTrace();
     * @param doc
     private void writeToXML(Document document,File filePath)
               XMLOutputter xmloutputter = new XMLOutputter();
                    try
                         FileOutputStream fileOutputStream = new FileOutputStream(filePath);
                         xmloutputter.output(document, fileOutputStream);
                         fileOutputStream.close();
                    catch (FileNotFoundException e)
                         e.printStackTrace();
                    catch (IOException e)
                         e.printStackTrace();
     public void getChildren(Element element)
                    if(!element.hasChildren())
                         return;
                    List childrenList = element.getChildren();
                    Iterator itr=childrenList.iterator();
                    while(itr.hasNext())
                         Element childElement=(Element) itr.next();
                         if(childElement.hasChildren())
                                   getChildren(childElement);
//                                   System.out.println("Name "+childElement.getName());
//                                   System.out.println("Value "+childElement.getText());
                                   if(childElement.getText().equals("hello") || (childElement.getName().equals("resource")))
                                        updateInfo(childElement,"New_Resource","AddedText");
     * @param childElement
     * @param string
     * @param string2
     private void updateInfo(Element element, String elementName, String value)
          element.setName(elementName);
          element.setText(value);          
static public void main(String[] args)
XMLWriter xmlWriter=new XMLWriter();
xmlWriter.update(new File("c:/sample.xml"));
After execution the file will be changed to
<URLConstructor>
<application name="cp_outage">
<New_Resource>AddedText</New_Resource>
<value>val</value>
</application>
<application name="cp_outage">
<New_Resource>AddedText</New_Resource>
<value>val</value>
</application>
</URLConstructor>
Regards,
Maheswar

Similar Messages

  • How do I change the value of the Order By field in an MP3 file?

    good day Guys,
    I'm new here in the forum and I have a question that is "breaking" the order of my songs and my head too ...
    How do I change the value of the Order By field in an MP3 file?
    for example, in the photo below, the song "The Bad Touch" is coming BEFORE the song "A Little Respect", this happens because of the Name field to Order ...
    My "problem" is in the photo below, the "tab" Order, iTunes will not let me erase what is written in the "Sort As" ...
    I've been "collecting" several songs over SEVERAL years
    I'm currently with more than 1,500 songs on HD and several backups on DVDs and external hard drives and I think, download it again, will be a "little" laborious and boring
    I DON'T WANT to be filling the tags of songs (name, singer, CD, year, etc.)
    I have used the program Mp3tag to remove the music tags
    I've tried to download the song again and go through Mp3tag program as if it were a new song
    I've changed the music access permissions, folder and even entire hard drive (my Windows has only one user)
    I've tried to catch the song again, from a previous backup
    I've tried to create a copy of the file at iTunes own
    I've tried to create a new music file in a music recording program
    NOTHING of what I did above resolved
    -> I'm using Windows 7 64-bit Ultimate
    -> I'm using iTunes 12.1.1.4 for 64-bit Windows
    -> My iPad is the third generation (a little old, I know) with Wi-Fi and 3G, but it has served me VERY WELL
    -> The iPad is updated with iOS 8.1.3 and is not unlocked
    I'm sorry for the GIANT post, but well, I think I could explain as much as possible and for my English, too bad, by the way, I'm from Brazil and I'm using Google translator
    Hugs and await answers,
    Paulo Ricardo

    hello turingtest2,
    my language is Brazilian Portuguese (there are some little differences between Brazilian Portuguese and the Portuguese of Portugal, but nothing that interferes with the script)
    at this time, I'm managing songs manually, I don't know if this will interfere with the execution of a script or command
    as I picked up this iPad recently, I still don't understand how to set up the iTunes Music Library and the person who gave me, told me it was easier to put the songs manually
    as you said, in my iTunes, 95% of the songs are with ALL the blank fields, including the Name to Sort field, only songs that begin with a/an/the standing with the Name to Sort field filled with name of the song, without the a/the
    I do not know what to do...
    I want to learn to ride the iTunes Music Library, that my work is less
    you know any site that teaches create/manage my iTunes Music Library?
    this site/forum may be in English, no problem
    thanks for all your help and the help of others who responded to my request

  • How can I change the value used in a Criteria Rule in a Subscription agent

    Dear mobile heroes.... How can I change the value used in a Criteria Rule in a Subscription agent in a quality environment? (CRM 70 SP04).
    I have built a subscription agent in dev, which uses a static where clause to filter activity_object on VKORG. I need to filter on a different VKORG value in the quality environment, but when I change the value of the sales org, the change is not saved. I have tried this even non 'activated'  subscription agents, but it still does not appear possible to change and save the changes.
    This poses a big problem because the VKORG value I want to use does not even exist in the dev environment, so I could not even rebuild in dev and transport the new agent to quality. Can anyone help me?

    Too old to care now.

  • How does one change the font size for folders and/or file lists in the Bookmarks Library?

    How does one change the font size for folders and/or file lists in the '''Bookmarks''' Library?
    Since the upgrade to version 9.0.1 of Firefox, the Bookmarks feature changes are confusing me. They seem to be confusing themselves as well. The list of bookmarks has changed. The font size is so small that my aging eyes cannot read it without fogging the screen with my breath. Some folders are out of alphabetical order (where I know they were previously good), and some are missing altogether (folders to which I frequently add references).
    As for missing or deranged files or folders, was there something that I should have done or now need to do to recover those after the upgrade (or before)?
    With regard to font size,
    1. there is no “Edit Bookmarks” or like option to edit the list in this version
    2. the “zoom” option in the “view” list of functions is greyed out when in “Show All Bookmarks” window
    3. expanding the browser window has no effect on font size
    4. “Preferences” settings for font size has no effect in that window either, including advanced settings
    5. “Help” offers none that I can find.
    Can any of you Help?!?

    Maybe this extension helps:
    *Theme Font & Size Changer: https://addons.mozilla.org/firefox/addon/theme-font-size-changer/

  • How do you change the value of the primary key in the CMP?

    Hi,
    The cmp that I built has the phone as the Prikey field.
    The bean has the get/set methods. The servlet invokes the cmp
    to change the phone number. How could I do that?
    The setXXX for the priKey can not be called outside of the ejbCreate() so
    how would I change the prikey value of a particular row of the table. Do I have
    to remove the bean then create the new bean with the new priKey value?
    Please help,
    Thanks

    Thanks Mona
    Tom.
    Mona Ramlawi <[email protected]> wrote:
    Hi TOM,
    The primary key is the unique identifier of an Entity Bean.
    To change it, you have to remove the bean instance = database delete
    then you have to create a new one with the new primary-key value =
    database insert.
    Hard luck tom, maybe you should consider changing your primary key. You
    can easily change it to an AutoNum
    Tom wrote:
    Hi,
    The cmp that I built has the phone as the Prikey field.
    The bean has the get/set methods. The servlet invokes the cmp
    to change the phone number. How could I do that?
    The setXXX for the priKey can not be called outside of the ejbCreate()so
    how would I change the prikey value of a particular row of the table.Do I have
    to remove the bean then create the new bean with the new priKey value?
    Please help,
    Thanks

  • How can I change the value in a textfield

    I have some problems with the component textfield.
    For example, if I want to change the value when ocurr event, how can i do that?
    I need that in a several textfields appear the values of a select sql consult, plz help me its more important

    Hi,
    I have tried it works for me, Here is what I did
    1. Drop a textfiled on to the page
    2. Drop a button
    3. In Button action handler put the code as
    textField1.setValue("Creator");
    4.deploy and run the application
    Even I tried with more than one textfield and it works fine.
    ie , textField2.setValue("Sun Java Studio Creator"); in the same button action.
    MJ

  • How can I change the value of "colorscheme" parameter at runtime?

    Hello all
    Can I change the value of colorscheme at runtime ?
    This parameter can be config in formsweb.cfg file.
    I want to put this colorscheme as "User Preferences" in my application.
    Please help me if some one knows how to achieve this.
    From
    Chirag Patel (Nairobi)

    Hi,
    Try the following. This might help you.
    Edit you Formsweb.cfg file with following code (add the code to your file):
    [look1]
    separateFrame=True
    lookandfeel=Generic
    [look2]
    separateFrame=True
    lookandfeel=Teal
    [look3]
    separateFrame=True
    lookandfeel=Khaki
    and save your file.
    Now, when you call your form in the address bar look for " ? ". After " ? " add config=look1 now your form will take Genric look & feel.
    or
    config=Teal now your form will take Teal look & feel.
    That's how you can change the look & feel at run time.
    Hope that you understand what i want to say......

  • How can I change the value in a stepper cell

    In Numbers 2.3 I was able to change the value in a stepper cell by highlighting the value and using the arrow keys to increase or decrease the value. This was a very easy way to input data. Numbers 3.0 when I do this is moves to another cell
    Is there a way to get the use of the arrow keys to change the values in 3.0?
    Thanks

    I use number to keep a running daily tally of the productivity of the members of my group. As each member performs certain tasks, all I had to do was click the up arrow and add to it. IE Jim made 4 widgets yesterday and then made 4 today, I would just hit the up 4 times total 8.
    I guess the good news is when I upgraded to Numbers 3.0, It did not delete Numbers 2.3
    I will continue to use 2.3 until it no longer works, I guess
    Thanks for the replies

  • How can i change the order of  elements in a JList ?

    Hi,
    i would like to know if it is possible to change the order of elements in a JList ? Maybe i could use drag and drop to do this ?
    Thanks for answering.

    The simplest way is probably to implement ur own listmodel and then supply the listitems in the order u want. Example:
    class MyListModel
              extends AbstractListModel
              implements ContactListListener {
    public int getSize() {
    return listitemcount;
    public Object getElementAt(int index) {
    //here u return the item for the index at the list
    //if u for example have an array of strings
    //which u wanna have listed in reversed order (reversed to
    //the order in the array, u could do
         return myarray[getSize()-index];
    }

  • How can I change the values of Withholding tax grid in MIRO trx

    Hi everyone,
    I am trying to change WT_WITHCD field in MIRO's Withholding tax grid by using  MRM_HEADER_CHECK. I developed the following code but the changes didn't appear in the screen:
            LOOP AT i_rbkpv-h_rbws
            INTO    r_h_rbws.
              d_index = sy-tabix.
              READ TABLE i_zrmm_tnd_wht
              INTO r_zrmm_tnd_wht
              WITH KEY   invoice_type = r_h_rbws-witht
                         invoice_ret  = r_h_rbws-wt_withcd
              BINARY SEARCH.
              IF sy-subrc EQ 0.
    <b>            r_h_rbws-wt_withcd = r_zrmm_tnd_wht-debit_ret.</b>
    <b>            MODIFY i_rbkpv-h_rbws FROM r_h_rbws INDEX d_index.</b>
              ENDIF.
            ENDLOOP.
          ENDIF.
        ENDIF.
    <b>    ASSIGN ('(SAPLMR1M)RBKPV') TO <fs_rbkpv>.</b>
        IF sy-subrc EQ 0.
    <b>      <fs_rbkpv> = i_rbkpv.</b>
        ENDIF.
    Do you have any idea?
    Thanks in advance!
    Regards.
    Leo.

    Hi Naimesh,
    I tried to assign only the withholding tax but it didnt work...
    It modified the value but then it doesn't appear in the screen...
    Thanks in advance for your help.
    Leo.

  • How Can i change the value from A to B!!!

    Hi,
    In table VBAP for VBELN 4 the entry for VSTEL we see the value as SHIP I want it to be changed to FEXP?
    please respond at the earliest

    Hi,
    tHANKS FOR THE INFORMATION. What is the transaction code for this?
    Can you give me an idea in clear way. Thanks in advance.

  • How can i change control values based on a read-in file?

    I understand that controls only have outputs and indicators only have inputs, but I'm looking for some help creating code that has the capabilities of both. Specifically and ideally, my front panel would allow the user to select an old spreadsheet file to read, and those values from the spreadsheet would be displayed. The user could then tweak those values to their liking, and then submit those tweaked values as a new file/overwrite the old/etc. The important part is the the indicating and controlling of the read in values.
    thanks
    Solved!
    Go to Solution.

    Using a local variable works well if I want to view and change a single value, but I want to view and change a few different values that are to be read in from a speadsheet. I have tried creating an array and then inserting numeric controls into the array, on the front panel. The code will run, but there is no output to the array control. I can't for the life of me pinpoint where the issue is.
    Is inserting multiple numeric controls into the array, not a good idea? Should I just create 3 separate numeric controls and local variables to get around this?
    Message Edited by csmrunman on 09-16-2009 11:48 AM
    Attachments:
    controlasindicator.vi ‏7 KB

  • How do I change the default "open with" application for certain files??  I've tried EVERYTHING!!

    Hi guys!
    PLEASE PLEASE PLEASE someone help me!!!  I have been trying for ages to change this!!  It is SO frustrating!!  I recently bought a new MacBook Pro 13" after my old one (2009) got stolen.  Went from Snow Leopard to 10.8.2, restored all my apps and files etc (thanks goodness I backed up).
    NOW I have a problem!  All my .xls and .doc files open up with Pages and Numbers, and not OpenOffice.  NO MATTER WHAT I DO!!!
    The basic "change open with" "always use this application" story doesn't work.  It always reverts back to Pages or Numbers.  I also tried a Terminal Command I found on this forum from way back in 2010.  But that doesn't work either.  I also tried a .pst deletion but I don't have a .com.apple.launches.pst file!
    PLEASE GUYS.  PLEASE!  SOMEONE HELP ME HERE!!!!!!!!!!  It's driving me nuts!!!!!  LIKE NUTS!!!!!!!

    Here is what I would do:
    Update OpenOffice to the latest Mac OS X version for improved compatibiity with Mountain Lion.
    Boot into Safe Mode to clear caches, verify/repair permissions, and rebuild font database, etc.
    Reboot, holding down the shift key until you see a progress bar on the grey boot screen.
    At the login prompt, and before you click the right arrow button to login, press and hold shift again.
    Reboot normally.
    Now follow sig's advice, and specify OpenOffice as the can opener for Word and Excel files.
    If OpenOffice installs into a /Applications folder as individual writer, and sheet applications, then use these names respectively for Word and Excel files.
    LibreOffice (what I use) only offers the LibreOffice application choice.

  • How do you change the default Save As location for PDF files?

    I have not been able to find a straight answer anywhere.  I am scanning documents to my email then I need to save them.  Instead of saving them to the last folder I saved a file in, (which would be logical) it saves them in a temporary files folder EVERY time.  I have seen answers about how to change the folder when saving documents from a web browser but that is not what I am doing.  Someone please help!  I have thousands of files to save and this is incredibly frustrating. Agh!!!

    Is it your scanning software that does that, or Adobe Reader?
    Anyway, I don't know how to change the default change destination for either of it.
    I have had a similar problem with an entirely different software, so what I did is create a shortcut to the destination folder in my Temp folder, so a simple double-click when saving gets met quickly to that target folder.

  • How do you change the color of a jpeg or tiff file (from b/w to color)?

    I recently was given Creative Suite 5 so I can create ads, flyers, etc.  I am trying to change our logo from a black and white to PMS 541 Blue.  I was able to change the text color, but for some reason it will not stick to the images.  Most of the graphics/logos I use are jpegs, tiffs or eps files.   I am still learning PS, but so far I have found it hard to use and would appreciate any help so I can at least get this very easy flyer out.  Will definetly be a regular on this forum until I can figure it out. Thanks in advance for any help.

    PSB Mktg wrote:
    ...If I did define my PMS Spot Channel then I did it by accident
    You can tell if you have defined it by looking at your Channels palette. I would wager that it is not there based on your description.
    http://www.planetphotoshop.com/working-with-spot-color-channels.html
    As you have the suite, you might look at using Illustrator for logos. Photoshop is rarely used for logos.

Maybe you are looking for

  • MIGO ERROR IN STO

    Hi,       While creating MIGO in STO process,we are getting error "Maintain Vendor for the excise invoice"this is the first time we are doing in our company STO-2PLANT in 1Sales Org            Process 1)ME21N 2)VL01N 3)VF01 4)J1IIN-for Excise posting

  • Statistical posting to Cost center in a CCA report.

    Dear, I am posting AP vendor invoice with one cost element which categorized as a primary cost with cost center. However CCA report S_ALR_87013611 shows figures 'sometimes' under statistical acct.  Can you tell me where i should go through? Regards,

  • Asset Reversal AB08 and Issue in Dep run

    Hi folks, I have done an inter company asset transfer Using ABT1N -Asset got successfully transferred and after posting 2 depreciations runs, i have cancelled the asset transfer via AB08. The asset posted values tab is showing me a reversal of previo

  • How to control sequence of Function Modules processed in update task?

    Hello, I've some questions regarding following process that needs to be extended: a Function Module (FM) 'Save' calls several other FM e.g. 'X1' and 'X2' in update task. Those update modules are independent from each other and write data to the datab

  • Disable toolbar in SALV_WD_TABLE

    hi, We are implementing Performance management process based on EHP4. We are using standard SAP delivered WD ABAP applications. On the manager's view there is table to show all appraisal form of his sub ordinates. This table is created through SALV_W