JAVA开发系列之:xml技术

1:DTD规范html

dtd规范:规范xml格式,规定xml标签及属性名称。java

JAVA源码:mysql

<?xml version="1.0" encoding="UTF-8"?>
//DTD规定
<!ELEMENT books (book*)>
<!DOCTYPE book [
<!ATTLIST book id CDATA #IMPLIED>          //id规定
<!ELEMENT book (name,autour,price)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT autour (#PCDATA)>
<!ELEMENT price (#PCDATA)>
]>sql

2:DomFactory解析oracle

JAVA源码:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse("books.xml");
        NodeList list = doc.getElementsByTagName("book");
        for(int i = 0 ;i<list.getLength();i++){
            NodeList lists = list.item(i).getChildNodes();
            //获得属性
            Element ele = (Element) lists;
            System.out.println(ele.getAttribute("id").toString());
            for(int j = 0 ;j<lists.getLength();j++){
                //使用判断获得属性
                if(lists.item(j).getNodeName().equals("name")){
                //lists.item(j).setNodeValue("语文");
                //lists.item(j).setTextContent("语文");
                System.out.println("图书名字是:"+lists.item(j).getTextContent());
                }
            }
        }
    }ui

3:Dom4j解析编码

JAVA源码:
File file = new File("books.xml");
        SAXReader saxReader = new SAXReader();
        Document doc = saxReader.read(file);
        Element ele1 = doc.getRootElement();
        Iterator ite1 = ele1.elementIterator();
        while(ite1.hasNext()){
            Element ele2 = (Element)ite1.next();
            Iterator ite2 = ele2.elementIterator();
            System.out.println(ele2.attribute("id").getValue());
            while(ite2.hasNext()){
                Element ele3 = (Element)ite2.next();
                if(ele3.getName().equals("name")){
                    System.out.println("图书名字是:"+ele3.getText());
                }
            }
        }
    }orm

4:xml文件修改xml

       //保存文档(DomFactory读xml文件)
        TransformerFactory  tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        DOMSource ds = new DOMSource(doc);
        //设置编码格式
        t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        StreamResult rs = new StreamResult(new FileOutputStream("books.xml"));
        //转换成文档
        t.transform(ds, rs);htm

附:xml文件内容,文件名称book.xml <?xml version="1.0" encoding="UTF-8"?> //DTD规定 <!ELEMENT books (book*)> <!DOCTYPE book [ <!ATTLIST book id CDATA #IMPLIED>          //id规定 <!ELEMENT book (name,autour,price)> <!ELEMENT name (#PCDATA)> <!ELEMENT autour (#PCDATA)> <!ELEMENT price (#PCDATA)> ]> <!—XML内容 --> <books>     <book id="0001">         <name>java基础</name>         <autour>青哥</autour>         <price>20.5</price>     </book>     <book id="0002">         <name>html基础</name>         <autour>赵五</autour>         <price>32.5</price>     </book>     <book id="0003">         <name>oracle基础</name>         <autour>小陈</autour>         <price>32.0</price>     </book>     <book id="0004">         <name>mysql基础</name>         <autour>张三</autour>         <price>15</price>     </book> </books>

相关文章
相关标签/搜索