The use of XML and JSON

Source: Internet
Author: User

I. When using JSON, we need to introduce a dynamic link library Litjson Baidu can be found, put in the program directory can be used

1. First we create an XML file

public void Createxml ()
{
The path to the XML save, placed below the path of the asset
String filepath = Application.datapath + @ "/test.xml";
Determine if the file is present under the current path
if (! File.exists (filepath))
{
Creating an XML document instance
XmlDocument xmldoc = new XmlDocument ();
Create the root node, which is the top level node
XmlElement root = Xmldoc.createelement ("transforms");
Continue to create the next layer node
XmlElement Elmitem = xmldoc.createelement ("rotation");
Set the two attribute ID of the node with the name
Elmitem.setattribute ("id", "0");
Elmitem.setattribute ("name", "Colo");


Continue to create the next layer node

XmlElement rotation_x = xmldoc.createelement ("X");
Set the node value
Rotation_x.innertext = "0";

XmlElement rotation_y = xmldoc.createelement ("Y");
Set the node value
Rotation_y.innertext = "1";

XmlElement rotation_z = xmldoc.createelement ("Z");
Set the node value
Rotation_z.innertext = "2";
Rotation_z.setattribute ("id", "1");

Add the created node to the XmlDoc
Elmitem.appendchild (rotation_x);
Elmitem.appendchild (rotation_y);
Elmitem.appendchild (rotation_z);
Root. AppendChild (Elmitem);
Xmldoc.appendchild (root);
Save XML to Local
Xmldoc.save (filepath);
Debug.Log ("Create Xml OK!");

}
}

2. We then have the ability to update the XML content based on what we have created.

public void Updatexml ()
{
String filepath = Application.datapath + @ "/test.xml";
if (file.exists (filepath))
{
Creating an XML document instance
XmlDocument xmldoc = new XmlDocument ();
Read the XML according to the path
Xmldoc.load (filepath);
Get all child nodes below the transform
XmlNodeList nodelist = Xmldoc.selectsinglenode ("Transforms"). ChildNodes;
Traverse all child nodes
foreach (XmlElement item in nodelist)
{
Get node with id = 0 in child node
if (item. GetAttribute ("id") = = "0")
{
Update child nodes
Item. SetAttribute ("id", "520");
Continue traversing the child nodes of the Node ID = 0
foreach (XmlElement Xe in item. ChildNodes)
{
if (XE. Name = = "Z")
{
Xe. SetAttribute ("id", "521");
Xe. InnerText = "Coloooo";
}
}
Break
}
}
Xmldoc.save (filepath);
Debug.Log ("Update Xml ok!");
}
}

3. We can also add to the XML again, similar to the one created at the beginning

public void Addxml ()
{
String filepath = Application.datapath + @ "/test.xml";

if (file.exists (filepath))
{
XmlDocument xmldoc = new XmlDocument ();
Xmldoc.load (filepath);

XmlNode root = Xmldoc.selectsinglenode ("transforms");
XmlElement elemnew = xmldoc.createelement ("rotation");
Elemnew.setattribute ("id", "1");
Elemnew.setattribute ("name", "Xolooo");

XmlElement rotation_x = xmldoc.createelement ("x");
Rotation_x.innertext = "0";
Rotation_x.setattribute ("id", "0000");
XmlElement rotation_y = xmldoc.createelement ("y");
Rotation_y.innertext = "1";
XmlElement rotation_z = xmldoc.createelement ("z");
Rotation_z.innertext = "2";

Elemnew.appendchild (rotation_x);
Elemnew.appendchild (rotation_y);
Elemnew.appendchild (rotation_z);
Root. AppendChild (elemnew);
Xmldoc.appendchild (root);
Xmldoc.save (filepath);
Debug.Log ("Addxml ok!");
}

}

4. Delete Processing of our previous XML

public void Deletexml ()
{
String filepath = Application.datapath + @ "/test.xml";
if (file.exists (filepath))
{
XmlDocument xmldoc = new XmlDocument ();
Xmldoc.load (filepath);

XmlNodeList nodeList = Xmldoc.selectsinglenode ("Transforms"). ChildNodes;
foreach (XmlElement item in nodeList)
{
if (item. GetAttribute ("id") = = "1")
{
Item. RemoveAttribute ("id");
}
foreach (XmlElement Xe in Item)
{
if (XE. Name = = "Z")
{
Xe. RemoveAll ();
}
}
}
Xmldoc.save (filepath);
Debug.Log ("Deletexml ok!");
}
}

5. Overall output of XML

public void Showxml ()
{
String filepath = Application.datapath + @ "/test.xml";
XmlDocument xmldoc = new XmlDocument ();
Xmldoc.load (filepath);

XmlNodeList nodeList = Xmldoc.selectsinglenode ("Transforms"). ChildNodes;
foreach (XmlElement item in nodeList)
{
Debug.Log ("Name:" + item.) Name + "\ n" + "Attribute:" + Item. GetAttribute ("name"));
Debug.Log ("Name:" + item.) Name);
foreach (XmlElement Xe in item. ChildNodes)
{
if (XE. Name = = "Y")
{
Debug.Log ("Y of" + item. GetAttribute ("name") + ":" + XE. InnerText);
}
}
}
Debug.Log ("all =" + Xmldoc.outerxml);
}

6.Json string Customization (string definitions, as well as program definitions, described in step 7), and the output to JSON, you need to highlight the JSON format

public void Resolvejson ()
{
Defining JSON strings
String str = @ "
{
"" Name "": "" coqoooo "",
"Age" ": 20,
"Birthday" ":" "1995-1-1",
"Thu" ": [
{
"" Url "": "" HTTP1 "",
"Height" ": 251
},
{
"" Url "": "" HTTP2 "",
"Height" ": 252
}

]
}";

Analytical
Jsondata JD = Jsonmapper.toobject (str);
Debug.Log ("name =" + jd["name"]);
Debug.Log ("age =" + jd["age"]);
Debug.Log ("Birthday =" + jd["Birthday"]);
Jsondata Jditems = jd["Thu"];

for (int i = 0; i < jditems.count; i + +)
{
Debug.Log ("url =" + jditems[i]["url"]);
Debug.Log ("height =" + jditems[i]["height"]);
}

}

7.Json created the second way, this way applies to a small number of item, if more suggestions or step 6 way

public void Mergerjson ()
{
Synthetic JSON strings, which are then synthesized and then output
StringBuilder sb = new StringBuilder ();
Jsonwriter writer = new Jsonwriter (SB);

Writer. Writeobjectstart ();

Writer. Writepropertyname ("Name");
Writer. Write ("CQ");

Writer. Writepropertyname ("Age");
Writer. Write (26);

Writer. Writepropertyname ("Girl");

Writer. Writearraystart ();

Writer. Writeobjectstart ();
Writer. Writepropertyname ("name");
Writer. Write ("Rourou");
Writer. Writepropertyname ("Age");
Writer. Write (22);
Writer. Writeobjectend ();

Writer. Writeobjectstart ();
Writer. Writepropertyname ("name");
Writer. Write ("Loulou");
Writer. Writepropertyname ("Age");
Writer. Write (23);
Writer. Writeobjectend ();

Writer. Writearrayend ();

Writer. Writeobjectend ();
Debug.Log (sb.) ToString ());

}

The use of XML and JSON

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.