Data store-Customize JSON string write file to read data in XML format

Source: Internet
Author: User

There are three ways in which data storage is generally

1>, database

2>, file

3> the network

Here I introduce the JSON format and the XML format of the file storage

Let's talk about the format of commonly used file storage data

1>,json format

2>,xml format

I think the difference between these two formats is that JSON is suitable for storing small data, and XML is suitable for storing big data.

In Cocos2d-x, if we are going to use JSON first we need to first import a JSON library: Libjson

The demo is as follows:

To add a Libjson header file

To create data in JSON format:

Here with Jsonnode, define a parent node n, and then define a node array c, named Childnode, and then press the data into the child node, the node is pressed into the array, and then the array is added to the parent node

This creates a JSON string

Let's look at the parse JSON again:

Instead of parsing the JSON string into a file, I use the write_formatted () function to convert it to a JSON-formatted string

Next create and write a new file:

If there is a file in the sandbox, it will be written directly to the file, and if it does not, the file is created and then written

Read the file again:

Here are relative to the sandbox, not relative to the engineering resources

Re-initialize the function to call these methods.

Look at the results of the operation:

So we can save the data in JSON format in the file.

XML file, I'm here to talk about its parsing.

First we create a new Cocos2d-x project and then import an XML library: TinyXML

Add a Test.xml file to the project:

We define a person class to receive and print each set of data:

Person.h

Person.cpp

We parse the XML inside the main class:

Finally, it is called directly in the main function.

Data store-Customize JSON string write file to read data in XML format

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.