• 请用java解析下xml


    Java   XML简介

    XML(EXtensible Markup Language)  可扩展标记语言

    可以说是一个文本文件

    作用数交互  配置应用程序

    Xml解析技术

    三种方式

    Dom 文档数据结构   多次访问  占用资源多 一次性将所有的节点

    一次性都都去到内存中

    Sax

    事件的解析  占用资源少

    Dom4j

    非常优秀的   功能 强大 开源的

    第一种解析方式  下面代码呢是Dom 的集中增删改的 样本

    Dom解析xml  导报org.w3c.dom

    写写代码吧!!!

    第一步:

    DocumentBuilderFactory     factory    =      DocumentBuilderFactory.newInstance(); //建立一个工厂  因为他是一个抽象的类 所有呢他不能new自己所以呢 只能点到自己的静态方法

    第二步:

    DocumentBuilder buile = factory = factory.newDocumentBuilder();

    第三步:

    Document docuent =buile.parse(“”);//填写的呢是解析xml的具体地址

    第四步:

    //根据节点  进行数据解析    

    NodeListt list = document.getElementsByTayName(“book”);  //获取节点

    //NodeList  是一个集合 所以呢他又他的长度  list.getLength();

    那么我们呢就遍历他的的节点并打印出出来

    第四步:

    For(int i = o ;i<list.getLength();i++){

    Element book = (Element)List.item(i);

    String attribut = book.getAttribute(“typeId”);   //寻找节点的参数吧  不知道怎么说

    String bookId = book.getElenmentsByTagName().item(0).getTextcintent();

    //然后呢用system打印出来 这样呢xml就写出来了

    System.out.println(attribut );

    }

    package ioDomeTwo;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.transform.Result;
    import javax.xml.transform.Source;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    
    public class MyMain {
        public static void main(String[] args) throws Exception {
    
        }
        
        
        
         public static void sadf() throws Exception{
    
                DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();//创建一个工厂
                DocumentBuilder builde = factory.newDocumentBuilder();
                Document document = builde.parse("E:/eclipseSpace/javaDome/src/ioDomeTwo/DomeOne.xml");
                //System.out.println(document);//打印内存地址
                //根据xml节点 数据解析
                NodeList list = document.getElementsByTagName("book");//获取节点
                for (int i = 0; i < list.getLength(); i++) {
                Element book = (Element)list.item(i);
                String attribut = book.getAttribute("typeId");
                String bookId = book.getElementsByTagName("bookId").item(0).getTextContent();
                String bookName = book.getElementsByTagName("bookName").item(0).getTextContent();
                String bookPrice = book.getElementsByTagName("bookPrice").item(0).getTextContent();
                String bookAuthor = book.getElementsByTagName("bookAuthor").item(0).getTextContent();
                System.out.println(attribut);
                System.out.println(bookId);
                System.out.println(bookName);
                System.out.println(bookPrice);
                System.out.println(bookAuthor);
                System.out.println("--------------");
             }
         }
         //增加
         public static void asf() throws Exception{
                 DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();//创建一个工厂
                DocumentBuilder builde = factory.newDocumentBuilder();
                Document document = builde.parse("E:/eclipseSpace/javaDome/src/ioDomeTwo/DomeOne.xml");
                //构造个节点
                Element book = document.createElement("book");
                book.setAttribute("typeid", "3");
                Element bookId = document.createElement("bookId");
                book.setTextContent("003");
                Element bookName = document.createElement("bookName");
                book.setTextContent("西游记");
                Element bookPrice = document.createElement("bookPrice");
                book.setTextContent("15.8");
                Element bookAuthor = document.createElement("bookAuthor");
                book.setTextContent("吴承恩");
                book.appendChild(bookId);
                book.appendChild(bookName);
                book.appendChild(bookPrice);
                book.appendChild(bookAuthor);
                //将book节点和整篇文档建立关联
                document.getElementsByTagName("books").item(0).appendChild(book);
                //传输工程
                TransformerFactory transformFactory = TransformerFactory.newInstance();
                //传输器
                Transformer transformer = transformFactory.newTransformer();
                Source source = new DOMSource(document);
                Result resule= new StreamResult("E:/eclipseSpace/javaDome/src/ioDomeTwo/DomeOne.xml");
                transformer.transform(source, resule);
                System.out.println("ok");
         }
         
         //修改
         public static void upder() throws Exception{
             DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();//创建一个工厂
                DocumentBuilder builde = factory.newDocumentBuilder();
                Document document = builde.parse("E:/eclipseSpace/javaDome/src/ioDomeTwo/DomeOne.xml");
                
                Element book = (Element)document.getElementsByTagName("books").item(2);
                book.getElementsByTagName("bookPrice").item(0).setTextContent("99");
                //传输工程
                TransformerFactory transformFactory = TransformerFactory.newInstance();
                //传输器
                Transformer transformer = transformFactory.newTransformer();
                Source source = new DOMSource(document);
                Result resule= new StreamResult("E:/eclipseSpace/javaDome/src/ioDomeTwo/DomeOne.xml");
                transformer.transform(source, resule);
                System.out.println("ok");
         }
         //删除
         public static void remve() throws Exception{
             DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();//创建一个工厂
                DocumentBuilder builde = factory.newDocumentBuilder();
                Document document = builde.parse("E:/eclipseSpace/javaDome/src/ioDomeTwo/DomeOne.xml");
                
                Element book = (Element)document.getElementsByTagName("books").item(2);
                document.getElementsByTagName("books").item(0).removeChild(book);
                //传输工程
                TransformerFactory transformFactory = TransformerFactory.newInstance();
                //传输器
                Transformer transformer = transformFactory.newTransformer();
                Source source = new DOMSource(document);
                Result resule= new StreamResult("E:/eclipseSpace/javaDome/src/ioDomeTwo/DomeOne.xml");
                transformer.transform(source, resule);
                System.out.println("ok");
         }
    }
  • 相关阅读:
    毕业生的商业软件开发之路 C#语言简介
    [毕业生的商业软件开发之路]第一次使用VS.NET集成开发环境
    一种应用程序命令执行架构设计
    DCWriter 电子病历文档编辑器的 电子病历功能规范对照表
    [毕业生的商业软件开发之路]积累与创新
    WEB开发人员的微软技术战略
    PureMVC(AS3)剖析:吐槽
    走在网页游戏开发的路上(九)
    [服务器开发]可伸缩系统的设计模式(译)
    回合制页游
  • 原文地址:https://www.cnblogs.com/BaoWangZe/p/9099966.html
Copyright © 2020-2023  润新知