phone_info.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<phoneInfo>
<brand name="华为">
<!--<type name="p40"/>
<type name="mate30"/>
<type name="p40pro"/>-->
<!--文本节点 type的子节点-->
<type>
p40
</type>
<type>
mate30
</type>
<type>
p40pro
</type>
</brand>
<brand name="苹果">
<type name="iPhone11p"/>
<type name="iPhoneX"/>
</brand>
<!--<brand name="三星">
<type name="note20"/>
</brand>-->
</phoneInfo>
TextXML:
package cn.kgc.kb09;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
/**
* @Author: ChaoKeAiMuZhi
* @Date: 2020/8/7 13:54
* @Description:
**/
public class TestXML {
Document document;
public void setDocument(String xmlPath){
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder=factory.newDocumentBuilder();
document=builder.parse(xmlPath);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
/*Document document;
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
document=builder.parse("phone_info.xml");*/
//将xml文件转换为document文件
TestXML xml = new TestXML();
xml.setDocument("phone_info.xml");
//通过标签来获取节点集合NodeList,第i个节点是调用item(i)方法
//强转为Element元素
NodeList brands = xml.document.getElementsByTagName("brand");
Node item = brands.item(0);
Element e= (Element) item;
System.out.println(e.getAttribute("name"));
/*for (int i = 0; i < brands.getLength(); i++) {
Node n=brands.item(i);
if(n instanceof Element){
e= (Element) item;
System.out.println(e.getAttribute("name"));
}
}*/
NodeList hwTypes =e.getChildNodes();
for (int i = 0; i < hwTypes.getLength(); i++) {
Node n=hwTypes.item(i);
if(n instanceof Element){
Element type= (Element) n;
System.out.println(type.getAttribute("name"));
//System.out.println(type.getTextContent());
//按照节点的方式获取出来
Node firstChild = type.getFirstChild();
if(firstChild instanceof Text){
Text t=(Text) firstChild;
//trim去空格
System.out.println(t.getWholeText().trim());
}
}
}
/*//课堂作业
Node item2=brands.item(1);
Element e2= (Element) item2;
System.out.println(e2.getAttribute("name"));
NodeList iphoneTypes=e2.getChildNodes();
for (int i = 0; i < iphoneTypes.getLength(); i++) {
Node n=iphoneTypes.item(i);
if(n instanceof Element){
Element type=(Element)n;
System.out.println(type.getAttribute("name"));
}
}*/
//建立节点并且追加到根节点
Node root = xml.document.getElementsByTagName("phoneInfo").item(0);
Element sx=xml.document.createElement("brand");
sx.setAttribute("name","三星");
Element sxType=xml.document.createElement("type");
sxType.setAttribute("name","note20");
sx.appendChild(sxType);
root.appendChild(sx);
//保存更改
TransformerFactory tf=TransformerFactory.newInstance();
tf.setAttribute("indent-number", new Integer(4));
Transformer t=tf.newTransformer();
t.setOutputProperty(OutputKeys.ENCODING,"UTF-8");//标注的字符编码
t.setOutputProperty(OutputKeys.INDENT,"yes"); //标注换行
DOMSource source = new DOMSource(xml.document);
FileOutputStream fos = new FileOutputStream("phone_info.xml");
StreamResult sr = new StreamResult(new OutputStreamWriter(fos));//只有用writer才能缩进
t.transform(source,sr);
}
}
library.xml
<?xml version="1.0" encoding="utf-8" ?>
<books>
<!--是标签的属性,id是唯一标识,属性元素-->
<book id="1" name="金瓶梅" author="兰陵笑笑生">
<!--用子标签的形式,类似于类里面的属性,文本元素-->
<description>古典文学著作,阅读量很高</description>
</book>
<book id="2" name="三国志" author="陈寿">
<description>陈寿拍司马懿马屁的著作</description>
</book>
<book id="3" name="红楼梦" author="曹雪芹"/>
<!--XML对大小写敏感,标签顺序不能错-->
<BOOK><a></a></BOOK>
</books>
Student:
package cn.kgc.kb09.json;
import java.io.Serializable;
/**
* @Author: ChaoKeAiMuZhi
* @Date: 2020/8/7 16:24
* @Description:
**/
public class Student implements Serializable {
private int stuId;
private String stuName;
private String gender;
private double score;
public Student() {
}
@Override
public String toString() {
return "Student{" +
"stuId=" + stuId +
", stuName='" + stuName + '\'' +
", gender='" + gender + '\'' +
", score=" + score +
'}';
}
public Student(int stuId, String stuName, String gender, double score) {
this.stuId = stuId;
this.stuName = stuName;
this.gender = gender;
this.score = score;
}
public int getStuId() {
return stuId;
}
public void setStuId(int stuId) {
this.stuId = stuId;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
TestJson:
package cn.kgc.kb09.json;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
/**
* @Author: ChaoKeAiMuZhi
* @Date: 2020/8/7 16:24
* @Description:接收对象数据,转成JSON,再转移到另外的客户端,调用这个类型来解析它,从不同页面跳转
* 在不同方式里面解析JSON字符串0.
**/
public class TestJson {
public static void main(String[] args) {
Student s=new Student(1,"川普","男",55.5);
System.out.println(s);
//JSON json = new JSONObject();
String jsonString = JSON.toJSONString(s);
Object o = JSON.toJSON(s);
System.out.println(jsonString);
//Object o = JSON.parse(jsonString);
//Student t = JSON.toJavaObject((JSON) o, Student.class);
JSONObject jsonObject = JSON.parseObject(jsonString);
Student t1=JSON.toJavaObject(jsonObject,Student.class);
//System.out.println(t);
System.out.println(t1);
}
}