To read an existing XML method, there are the following steps:
1. Create SAXReader object (sr)
2. Parsing the SAXReader object into a Document object (sr.read("./src/day23/Student.xml")) is actually using the SAXReader object to read the Document object of a certain address Returns a Document object
3. Get the root element of the document (doc.getRootElement())
4. Get all child elements (root.getElements) under the root element and return a list < element > collection
5. Traverse the List set to obtain an attribute List of child elements getElement();
6. Get the text element ename. Of the element attribute getTxt();
public static void main(String[] args) { //1. Create SAXReader object SAXReader sr = new SAXReader(); try { //2. Parse the specified XML file Document doc=sr.read("./src/day23/Student.xml"); // System.out.println(doc); //Gets the root element of the file Element root=doc.getRootElement(); // System.out.println(root); //Get all child elements in the root directory List<Element> list = root.elements(); List<Student> ls = new ArrayList<Student>(); System.out.println(list); for(Element e:list){ Student s = new Student(); //Gets the child element of the specified name in the specified element Element eName=e.element("name"); //Gets the text content in the element String name = eName.getText(); //Gets the child element of the specified name (age) in the specified element Element eAge=e.element("age"); //Gets the text content in the element String age = eAge.getText(); //Gets the child element of the specified name (sex) in the specified element Element eSex=e.element("sex"); //Gets the text content in the element String sex = eSex.getText(); //Gets the id attribute represented by the e element Attribute a=e.attribute("id"); String id = a.getValue(); System.out.println(id); s.setAge(Integer.parseInt(age)); s.setName(name); s.setSex(sex); ls.add(s); } System.out.println(ls); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Create a new XML format file
1. Create an empty Document object
Document doc = DocumentHelper.createDocument();
2. Create a root directory
Element root = addElement("Class");
3 traverse the List to create the child directory
Element name = addElement("name");
4. Add text content to the son directory
name.addText();
5. Use a special stream XMLWriter to write the new Document file to the directory
xw.write();
The code is as follows:
ublic static void main(String[] args) { List<Student> list = new ArrayList<Student>(); list.add(new Student(1,"Zhang Sanfeng",70,"male")); list.add(new Student(2,"Zhang Cuishan",30,"male")); list.add(new Student(3,"Zhang Wuji",20,"male")); //1. Create an empty Document object Document doc = DocumentHelper.createDocument(); //2. Create a root element (Note: there is only one root element, so we only create one class) Element root = doc.addElement("class"); for(Student s:list){ //Create son element Element stu=root.addElement("stu"); Element name=stu.addElement("name"); name.addText(s.getName()); Element age=stu.addElement("age"); age.addText(Integer.toString(s.getAge())); Element sex=stu.addElement("sex"); sex.addText(s.getSex()); stu.addAttribute("id", Integer.toString(s.getId())); //Write content XMLWriter xw = null; try { FileOutputStream fos = new FileOutputStream("D:"+File.separator+"stu.xml"); xw = new XMLWriter(fos,OutputFormat.createPrettyPrint()); xw.write(doc); System.out.println("Write it out!"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { xw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
Today's assignment is to add content to the original text. It combines reading and writing slightly. The code is as follows
public static void main(String[] args) { List<Txt> ls = new ArrayList<Txt>(); Txt t1 = new Txt("Zhang San",18,"Renmin University of China"); Txt t2 = new Txt("Li Si",19,"Zhejiang University"); ls.add(t1); ls.add(t2); SAXReader sr = new SAXReader(); Document doc=null; XMLWriter xw = null; try { doc = sr.read("./src/Day23/Homework1.xml"); Element et = doc.getRootElement(); for(Txt t:ls){ Element stu = et.addElement("student"); Element name = stu.addElement("name"); name.addText(t.getName()); Element age = stu.addElement("age"); age.addText(Integer.toString(t.getAge())); Element shool = stu.addElement("shool"); shool.addText(t.getSchool()); FileOutputStream fos = new FileOutputStream("./src/Day23/Homework1.xml"); xw = new XMLWriter(fos,OutputFormat.createPrettyPrint()); xw.write(doc); System.out.println("Write it out!"); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { xw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Then there are some basic knowledge, left at the back
XML: extensible markup language, is a data storage language
(can be used to save and transfer data)
* benefits: you can directly use labels to explain the content, and you don't need too long words
* HTML: hypertext markup language
* - HTML is a defined tag for use
* <html>
* - XML is a freely defined label
*
* parsing method of XML:
* - SAX parsing
* SAX is line by line parsing, that is, parsing while loading
* benefits: low memory pressure because it is parsed line by line
* disadvantage: the node cannot be operated
* - DOM parsing:
It is to load all the contents according to the hierarchical order, and then parse them
* benefits: the content to be parsed in the middle of memory will be loaded into a tree structure, and then parsed, and the node can be operated
* disadvantages: high memory pressure and slow parsing speed
Other parsing methods: JDOM,DOM4J, etc
We usually use DOM4J parsing