Generate XML file with XMLWriter

19 views
Skip to first unread message

pyth...@gmail.com

unread,
Apr 9, 2019, 2:46:18 PM4/9/19
to CodenameOne Discussions
Hi,
I have some problems to generate an XML file, the file is created but the root tag is not written into the file when I call the writeXML(writer, root) method;

Here is the code :

try{
        //Element root
        Element root = new Element("root");
        String path = FileSystemStorage.getInstance().getAppHomePath();
        XMLWriter xw = new XMLWriter(false);    
        OutputStream os = FileSystemStorage.getInstance().openOutputStream(path+""+"test.xml");   
        OutputStreamWriter writer = new OutputStreamWriter(os);
        xw.writeXML(writer, root);
        os.close();
        writer.close();

}catch(....){
   .....
}

Any idea ?
Thank you.

Shai Almog

unread,
Apr 10, 2019, 12:45:33 AM4/10/19
to CodenameOne Discussions
I just tried this:

Form hi = new Form("XML", BoxLayout.y());
Button write = new Button("Write");
hi
.add(write);
write
.addActionListener(new ActionListener() {

   
@Override
   
public void actionPerformed(ActionEvent evt) {

       
Element root = new Element("root");

       
XMLWriter xw = new XMLWriter(false);
   
       
Log.p(xw.toXML(root));
   
}
});

hi
.show();


It printed out:
 <root />

pyth...@gmail.com

unread,
Apr 10, 2019, 5:29:37 AM4/10/19
to CodenameOne Discussions
Yes it works.

But how to generate XML file and write tag into with XMLWriter ?
With my code and toXML() method, that isn't work.

Shai Almog

unread,
Apr 10, 2019, 10:14:09 PM4/10/19
to CodenameOne Discussions

I'm guessing the problem you are experiencing is in reading the XML.

pyth...@gmail.com

unread,
Apr 11, 2019, 8:25:55 AM4/11/19
to CodenameOne Discussions
Yes, I want to create an XML file and save it to the phone. For the moment, it is saved in the .cn/ repertory of my PC and when I open it for check, it is empty and no tag is there

Shai Almog

unread,
Apr 11, 2019, 10:32:46 PM4/11/19
to CodenameOne Discussions
Try this:

                try(OutputStream os = FileSystemStorage.getInstance().openOutputStream(path+""+"test.xml");  OutputStreamWriter writer = new OutputStreamWriter(os)) {
                    xw.writeXML(writer, root);
                    writer.close();
                } catch(IOException err) {
                    Log.e(err);
                }

It looks like your code closes the output stream instead of the writer. This can discard unflushed bytes.
Reply all
Reply to author
Forward
0 new messages