Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Posts: 16
Registered: ‎06-27-2012
My Device: 9900
My Carrier: Orange
Accepted Solution

Modify and save XML back

Hi,

 

I have a little difficulties to do the following and you might share with me the best practice gentlemen. hereby what I need below:

 

1- I want to append new child in Document

2- save it back to xml

3- refresh screen in BB

 

Can anyone help please.

 

Thank you

Developer
Posts: 228
Registered: ‎10-01-2011
My Device: Passport

Re: Modify and save XML back

It depends on how your applicaiton functions, but assuming your process is this:

 

1) Open XML File

2) Display in UI

 

You could modify the file itself then refresh the UI using invalidate();

 

So are you asking about how to manipulate the XML? Or how to refresh the display?

 

---------------------------------------------------

if i helped you press the LIKE button

Contributor
Posts: 16
Registered: ‎06-27-2012
My Device: 9900
My Carrier: Orange

Re: Modify and save XML back Cany any one help please...

Hello dears

 

Im stuck here I need your kind help. Share with my code to do the following:

 

1- Update childNode

2- Save back to XML without using Transformer, TransformerFactory or Serializer

 

 

Thank you

Contributor
Posts: 16
Registered: ‎06-27-2012
My Device: 9900
My Carrier: Orange

Re: Modify and save XML back Cany any one help please... Im stuck

Please help me

Developer
Posts: 228
Registered: ‎10-01-2011
My Device: Passport

Re: Modify and save XML back Cany any one help please...

I am assuming you are loading the file as an XML Document. You find your node like this:

 

NodeList nl = doc.getElementsByTagName("Your Search Node");
for (int i=0; i<nl.getLength(); i++) {
        Node node = (Node)nl.item(i);
        node.setTextContent("your new value");
}

 

Then just save you rfile back:

 

FileConnection fc = (FileConnection) Connector.open(file:///store/home/user/MyXMLFile.xml",Connector.READ_WRITE);

 

if (!fileConnection.exists())
     fileConnection.create();

 

outputStream = fileConnection.openOutputStream();
outputStream.write(data);
outputStream.flush();

outputStream.close();

 

Catch your exceptions of course

 

--------------------------------------------------------

if i helped you press the LIKE button

Contributor
Posts: 16
Registered: ‎06-27-2012
My Device: 9900
My Carrier: Orange

Re: Modify and save XML back Cany any one help please...


 FileConnection fc = (FileConnection)Connector.open(fileN, Connector.READ_WRITE);
        
 // append child code here, when finished

dom.getDocumentElement().normalize();

 

OutputStream out = fc.openDataOutputStream();
             XMLWriter writer = new XMLWriter(out);
             writer.setPrettyPrint();
             writer.setPreserveSpacing();
             DOMInternalRepresentation.parse(dom, writer);

             out.Close();

 

 

My code has no error during RunTime but it does not update the file.

Can you help and tell me what I was missing

Contributor
Posts: 16
Registered: ‎06-27-2012
My Device: 9900
My Carrier: Orange

Please read my code XML writing and correct me

Please read my code XML writing and correct me

Developer
Posts: 228
Registered: ‎10-01-2011
My Device: Passport

Re: Please read my code XML writing and correct me

Are you writing to the same file or a new file?

 

Always add a flush to make sure everything is actually written to the file.

 

--------------------------------------------------------

if I helped you press LIKE button

Contributor
Posts: 16
Registered: ‎06-27-2012
My Device: 9900
My Carrier: Orange

Re: Please read my code XML writing and correct me

Hello,

Thanks for your kind reply. I did flushing, see my code below please and tell me why it is not saving to the same file.

 


             FileConnection fc = (FileConnection)Connector.open(fileN, Connector.READ_WRITE, true);
          
             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
             DocumentBuilder builder = factory.newDocumentBuilder();
             InputSource is = new InputSource();
             dom = builder.parse(is);
             dom.getDocumentElement().normalize();
         ======================================

Append Code is here afer modification, I want to save back to XML

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

 OutputStream out = fc.openDataOutputStream();
             XMLWriter writer = new XMLWriter(out);
             writer.setPrettyPrint();
             writer.setPreserveSpacing();
            DOMInternalRepresentation rep = new DOMInternalRepresentation();
             DOMInternalRepresentation.parse(dom, writer);
             out.flush();
             out.close();

Highlighted
Developer
Posts: 228
Registered: ‎10-01-2011
My Device: Passport

Re: Please read my code XML writing and correct me

Your comment;

 

"Append Code is here afer modification, I want to save back to XML"

 

Have you checked the element is being appended correctly?