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

Native Development

Reply
New Contributor
josepatience
Posts: 8
Registered: ‎12-18-2012
My Device: BB 10

How do I save a list item into an existing XML file

[ Edited ]

Hi,

I would like to know  how to save a list item using the XmlDatamodel into another xml file.

 

Thanks.

Developer
Zmey
Posts: 1,512
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: How do I save a list item into an existing XML file

This doesn't seem to be possible.

You'll have to extract the data item by item and write it to the file using QXmlStreamWriter or some other class.

 

  QFile file(filename);
  file.open(QIODevice::WriteOnly);

  QXmlStreamWriter xmlWriter(&file);
  xmlWriter.setAutoFormatting(true);
  xmlWriter.writeStartDocument();

  xmlWriter.writeStartElement("ELEMENT");
  xmlWriter.writeStartElement("SUBELEMENT");
  xmlWriter.writeTextElement("State", "state");
  xmlWriter.writeEndElement();
  xmlWriter.writeEndElement();

  xmlWriter.writeEndDocument();

  file.close();

Another way is to implement your custom datamodel to perform both XML parsing and saving. This is a cleaner approach but harder to implement.

 


Andrey Fidrya, @zmeyc on twitter
New Contributor
josepatience
Posts: 8
Registered: ‎12-18-2012
My Device: BB 10

Re: How do I save a list item into an existing XML file

Thanks for the response. this is what I have. This what I tried based on your reply can you please advise.

import bb.cascades 1.0

Page {
     actions: [
                // create navigation panel actions here
                ActionItem {
                    title: qsTr("Create a report")
                    onTriggered: {
                        imgView.imageSource = "asset:///images/picture1br.png"
                    }
                }
            ]
    Container {
        id: listCt
        // the childcount for the list item
       property alias listChildCount: chList.childCount
       property alias radioSelection: radioGp.selectedIndex
       ListView {
           id: chList
                   dataModel: XmlDataModel {
                       source: "models/Chapter2.xml"
                   }
                    
                   listItemComponents: [
                       ListItemComponent {
                           type: "header"
                           Header {
                               title: ListItemData.title
                       }
                       },
                       ListItemComponent {
                           type: "listItem"
                       Container {
                        layout: DockLayout {}
                        
                        LabelLabel {
                            text: ListItemData.title
                            bottomPadding: 50.0
                            leftPadding: 150.0
                        }// end of LabelLabel
                        RadioGroup {
                            id: radioGp
                            dividersVisible: false
                            preferredWidth: 150
                            minWidth: 149.0
                            Option {
                                id: option1
                                text: "H"
                            }
                            Option {
                                id: option2
                                text: "M"
                            }
                            Option {
                                id: option3
                                text: "L"
                            }
                            // When a new option is selected, we print the selection to the console.
                            onSelectedValueChanged: {
                                console.debug("New filling set: " + selectedValue);
                                if(selectedIndex >= 0) {
                               radioIndex = radioGp.selectedIndex
                            }//endif
                        }//end onSelected
                    }//end radioGroup          
                    Divider {} // at the end of each list item   
                } // end of Container
            } // end of second ListItemComponent
        ] // end of listItemComponents list
        // When an item is selected, update the text in the TextField
        // to display the status of the new item
           onTriggered: {
                var selectedItem = dataModel.data();
                var page = getSecondPage();
                  secondPage.xmlWriter.setAutoFormatting(true);
                  seondPage.writeStartDocument();
                  seconPage.xmlWriter.writeStartElement(selectedItem);
                    xmlWriter.writeTextElement("State", radioIndex);
                    xmlWriter.writeEndElement();
                    xmlWriter.writeEndElement();                  
                    xmlWriter.writeEndDocument();
                    file.close();          
            }// end onTriggered
             property Page secondPage
                function getSecondPage() {
                   if (! secondPage) {
                 secondPage = reportFile.createObject();
                  }
                  return secondPage;
              }
            attachedObjects: [
            ComponentDefinition {
            id: reportFile
            source: "../models/report.qml"
            }
            ]           
      } // end of ListView
   } 
}

 Thx