DOM解析XML 创建XML JSON解析 创建JSON

languages.xml



	
		Java
		Eclipse
	
	
		Swift
		Xcode
	
	
		C#
		Visual Studio
	

放在aseets下

Android Studio main下新建aseets


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

public class MainActivity extends AppCompatActivity {

    private TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (TextView) findViewById(R.id.text);
        try {
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = builderFactory.newDocumentBuilder();
            Document document = builder.parse(getAssets().open("languages.xml"));
            Element element = document.getDocumentElement(); // 获取根元素
            NodeList list = element.getElementsByTagName("lan");
            for (int i = 0; i < list.getLength(); i++) {
                Element lan = (Element) list.item(i);
                text.append(lan.getAttribute("id") + "\n");
                text.append(lan.getElementsByTagName("name").item(0).getTextContent() + "\n");
                text.append(lan.getElementsByTagName("ide").item(0).getTextContent() + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
    }

}

导包记得导w3c下的



创建XML

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

public class MainActivity extends AppCompatActivity {

    private TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (TextView) findViewById(R.id.text);
        try {
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = builderFactory.newDocumentBuilder();
//            Document document = builder.parse(getAssets().open("languages.xml"));
//            Element element = document.getDocumentElement(); // 获取根元素
//            NodeList list = element.getElementsByTagName("lan");
//            for (int i = 0; i < list.getLength(); i++) {
//                Element lan = (Element) list.item(i);
//                text.append(lan.getAttribute("id") + "\n");
//                text.append(lan.getElementsByTagName("name").item(0).getTextContent() + "\n");
//                text.append(lan.getElementsByTagName("ide").item(0).getTextContent() + "\n");
//            }
            Document newxml = builder.newDocument();
            Element languages = newxml.createElement("Languages");
            languages.setAttribute("cat", "it");

            Element lan1 = newxml.createElement("lan");
            lan1.setAttribute("id", "1");
            Element name1 = newxml.createElement("name");
            name1.setTextContent("Java");
            Element ide1 = newxml.createElement("ide");
            ide1.setTextContent("Eclipse");
            lan1.appendChild(name1);
            lan1.appendChild(ide1);
            languages.appendChild(lan1);

            Element lan2 = newxml.createElement("lan");
            lan2.setAttribute("id", "2");
            Element name2 = newxml.createElement("name");
            name2.setTextContent("Swift");
            Element ide2 = newxml.createElement("ide");
            ide2.setTextContent("XCode");
            lan2.appendChild(name2);
            lan2.appendChild(ide2);
            languages.appendChild(lan2);

            newxml.appendChild(languages);

            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty("encoding", "utf-8");
            StringWriter sw = new StringWriter();
            transformer.transform(new DOMSource(newxml), new StreamResult(sw));

            text.setText(sw.toString());

//        } catch (IOException e) {
//            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
//        } catch (SAXException e) {
//            e.printStackTrace();
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    }

}

http://download.csdn.net/detail/duanliuchang/9583476



JSON:JavaScript对象表示法(JavaScript Object Notation)


数据由逗号分隔

花括号保存对象

方括号保存数组


test.json

{
    "languages":[
        {"id":1,"ide":"Eclipse","name":"Java"},
	    {"id":2,"ide":"XCode","name":"Swift"},
	    {"id":3,"ide":"Visual Studio","name":"C#"}
    ],
    "cat":"it"
}


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            InputStreamReader isr = new InputStreamReader(getAssets().open("test.json"), "UTF-8");
            BufferedReader br = new BufferedReader(isr);
            String line;
            StringBuilder builder = new StringBuilder();
            while ((line = br.readLine()) != null) {
                builder.append(line);
            }
            br.close();
            isr.close();
            JSONObject root = new JSONObject(builder.toString());
            System.out.println("cat = " + root.getString("cat"));
            JSONArray array = root.getJSONArray("languages");
            for (int i = 0; i < array.length(); i++) {
                JSONObject lan = array.getJSONObject(i);
                System.out.println("----------------");
                System.out.println("id = " + lan.getInt("id"));
                System.out.println("name = " + lan.getString("name"));
                System.out.println("ide = " + lan.getString("ide"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}


创建JSON格式数据

            JSONObject root = new JSONObject();
            root.put("cat", "it");
            // {"id":1,"ide":"Eclipse","name":"Java"},
            JSONObject lan1 = new JSONObject();
            lan1.put("id", 1);
            lan1.put("ide", "Eclipse");
            lan1.put("name", "Java");
            // {"id":2,"ide":"XCode","name":"Swift"},
            JSONObject lan2 = new JSONObject();
            lan2.put("id", 2);
            lan2.put("ide", "XCode");
            lan1.put("name", "Swift");
            // {"id":3,"ide":"Visual Studio","name":"C#"}
            JSONObject lan3 = new JSONObject();
            lan3.put("id", 3);
            lan3.put("ide", "Visual Studio");
            lan3.put("name", "C#");

            JSONArray array = new JSONArray();
            array.put(lan1);
            array.put(lan2);
            array.put(lan3);

            root.put("languages", array);


http://download.csdn.net/detail/duanliuchang/9583543


你可能感兴趣的:(Android)