标签:roo 评测 ted 2.x style 直接 通过 ddc utf-8
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import org.jdom2.Attribute; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.output.XMLOutputter; public class JDOM01 { public static void main(String[] args) { Element student=new Element("student"); Attribute id=new Attribute("id", "001"); // 属性节点 Attribute aa=new Attribute("aa", "xx"); student.setAttribute(id); student.setAttribute(aa); Element name=new Element("name"); //元素节点名 name.setText("张三"); // 设置文本节点 student.addContent(name); Document document=new Document(student); XMLOutputter out=new XMLOutputter(); out.setFormat(out.getFormat().setEncoding("UTF-8")); try { out.output(document, new FileOutputStream("src/student2.xml")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
import java.util.List; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.input.SAXBuilder; public class JDOM02 { public static void main(String[] args) throws Exception{ SAXBuilder builder=new SAXBuilder(); // SAXBuilder Document document=builder.build("src/students.xml"); Element students=document.getRootElement(); List studentList=students.getChildren("student"); for(int i=0;i<studentList.size();i++){ Element student=(Element)studentList.get(i); String id=student.getAttributeValue("id"); String name=student.getChildText("name"); String sex=student.getChildText("sex"); String age=student.getChildText("age"); System.out.println("学号:"+id+";姓名:"+name+";性别:"+sex+";年龄:"+age); } } }
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; public class DOM4J01 { public static void main(String[] args) { Document document=DocumentHelper.createDocument(); //获取文档 Element studentElement=document.addElement("student"); //通过文档直接添加元素 studentElement.addAttribute("id", "001"); //添加属性 studentElement.addAttribute("aa", "xx"); Element name=studentElement.addElement("name"); //添加元素 name.setText("张三"); //设置元素值 Element sex=studentElement.addElement("sex"); sex.setText("男"); Element age=studentElement.addElement("age"); age.setText("20"); OutputFormat format=OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); try { XMLWriter writer=new XMLWriter(new FileOutputStream("src/student3.xml"),format); // 输出 writer.write(document); writer.close(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
import java.util.Iterator; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class DOM4J02 { public static void main(String[] args) throws Exception{ SAXReader saxReader=new SAXReader(); //SAXReader Document document=saxReader.read("src/students.xml"); Element rootElement=document.getRootElement(); Iterator iter=rootElement.elementIterator(); while(iter.hasNext()){ Element studentElement=(Element)iter.next(); System.out.println("学号:"+studentElement.attributeValue("id")); System.out.println("姓名:"+studentElement.elementText("name")); System.out.println("性别:"+studentElement.elementText("sex")); System.out.println("年龄:"+studentElement.elementText("age")); System.out.println("========="); } } }
XML —— Java 操作 XML 工具包( JDOM&DOM4J )
标签:roo 评测 ted 2.x style 直接 通过 ddc utf-8
原文地址:https://www.cnblogs.com/Cocoomg/p/9901673.html