简介 目录 评价 推荐
  • 冰断秋 2019-07-27
    关于测试结果的疑问

    因为 xml 文本大小会对测试结果有影响

    1回答·656浏览
  • HappySimon 2018-11-07
    jUnit测试结果怎么是这样?
    2回答·1225浏览
  • 慕UI1804229 2018-06-13
    为什么改了工作区间还是会出现这样的问题?

    看看details里有什么?

    1回答·1070浏览
  • 慕粉3932524 2018-05-11
    有源码下载吗?

    视频框的右下角有一个资料下载,在那里面

    1回答·480浏览
  • 慕粉2233593303 2018-01-21
    源代码有吗?
    2回答·1166浏览
  • 黑夜_K 2017-08-01
    java中导入org.jdom.Document和org.dom4j.Document冲突

    jdom和dom4j其中一个的包正常导入 另一个要加上类所在的包名

    像这样https://img4.mukewang.com/5b1cb9f60001c19a08730053.jpg

    2回答·11774浏览
  • 武HH 2017-07-25
    junit的使用,自定义的与idea生成的测试类,结果为什么有比较大的出入

    首先编译器不同,单位指标不同。还有一点,数据量太小了,不能说明所有问题

    1回答·2343浏览
  • 暴躁的代码 2017-07-14
    为什么不用SAX?

    可能不够灵活吧

    1回答·693浏览
  • 慕雪3627465 2017-01-06
    在做测速的时候, 为何我的控制台 把之前的解析xml的语句也输出了 怎么样避免呢

    你把你程序里面解析xml的打印语句全部去除就好了

    1回答·697浏览
  • 慕粉3170877 2016-09-01
    程序每运行一次,结果就会发生变化,并且还有异常,求大神解!
    package net.paoyun.test;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.List;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    import org.dom4j.DocumentException;
    import org.dom4j.io.SAXReader;
    import org.jdom2.Attribute;
    import org.jdom2.JDOMException;
    import org.jdom2.input.SAXBuilder;
    import org.jdom2.input.sax.SAXHandler;
    import org.junit.Test;
    import org.w3c.dom.DOMException;
    import org.w3c.dom.Document;
    import org.w3c.dom.NamedNodeMap;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;
    
    import net.paoyun.entity.Book;
     
    public class ParseTest {
     public void domXmlParser() {
      ArrayList<Book> bookLists = new ArrayList<Book>();
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      try {
       DocumentBuilder db = dbf.newDocumentBuilder();
       Document document = db.parse("f:\\books.xml");
       NodeList bookList = document.getElementsByTagName("book");
       for (int i = 0; i < bookList.getLength(); i++) {
        Node book = bookList.item(i);
        Book bookEntity = new Book();
        NamedNodeMap attrs = book.getAttributes();
        for (int j = 0; j < attrs.getLength(); j++) {
         Node attr = attrs.item(j);
         if (attr.getNodeName().equals("id")) {
          bookEntity.setId(attr.getNodeValue());
         }
        }
        NodeList childNodes = book.getChildNodes();
        for (int k = 0; k < childNodes.getLength(); k++) {
         if (childNodes.item(k).getNodeType() == Node.ELEMENT_NODE) {
          String name = childNodes.item(k).getNodeName();
          String value = childNodes.item(k).getFirstChild().getNodeValue();
          if (name.equals("name")) {
           bookEntity.setName(value);
          }
          else if (name.equals("author")) {
           bookEntity.setAuthor(value);
          }
          else if (name.equals("year")) {
           bookEntity.setYear(value);
          }
          else if (name.equals("price")) {
           bookEntity.setPrice(value);
          }
          else if (name.equals("language")) {
           bookEntity.setLanguage(value);
          }
         }
        }
        bookLists.add(bookEntity);
        bookEntity = null;
       }
      } catch (ParserConfigurationException e) {
       e.printStackTrace();
      } catch (SAXException e) {
       e.printStackTrace();
      } catch (IOException e) {
       e.printStackTrace();
      }
     }
     
     public void saxXmlParser(){
      SAXParserFactory factory = SAXParserFactory.newInstance();
      try {
       SAXParser parser = factory.newSAXParser();
       SAXHandler handler = new SAXHandler();
       parser.parse("f:\\books.xml", handler);
      } catch (ParserConfigurationException e) {
       e.printStackTrace();
      } catch (SAXException e) {
       e.printStackTrace();
      } catch (IOException e) {
       e.printStackTrace();
      }
     }
     
     public void jdomXmlParser() {
      ArrayList<Book> booksList = new ArrayList<Book>();
      SAXBuilder saxBuilder = new SAXBuilder();
      InputStream in;
      try {
       in = new FileInputStream("f:\\books.xml");
       InputStreamReader isr = new InputStreamReader(in, "UTF-8");
       org.jdom2.Document document = saxBuilder.build(isr);
       org.jdom2.Element rootElement = document.getRootElement();
       List<org.jdom2.Element> bookList = rootElement.getChildren();
       for (org.jdom2.Element book : bookList) {
        Book bookEntity = new Book();
        List<Attribute> attrList = book.getAttributes();
        for (Attribute attr : attrList) {
         String attrName = attr.getName();
         String attrValue = attr.getValue();
         if (attrName.equals("id")) {
          bookEntity.setId(attrValue);
         }
        }
        List<org.jdom2.Element> bookChilds = book.getChildren();
        for (org.jdom2.Element child : bookChilds) {
         if (child.getName().equals("name")) {
          bookEntity.setName(child.getValue());
         }
         else if (child.getName().equals("author")) {
          bookEntity.setAuthor(child.getValue());
         }
         else if (child.getName().equals("year")) {
          bookEntity.setYear(child.getValue());
         }
         else if (child.getName().equals("price")) {
          bookEntity.setPrice(child.getValue());
         }
         else if (child.getName().equals("language")) {
          bookEntity.setLanguage(child.getValue());
         }
        }
        booksList.add(bookEntity);
        bookEntity = null;
       }
      } catch (FileNotFoundException e) {
       e.printStackTrace();
      } catch (JDOMException e) {
       e.printStackTrace();
      } catch (IOException e) {
       e.printStackTrace();
      }
     }
     
     public void dom4jXmlParser(){
      ArrayList<Book> booksList = new ArrayList<Book>();
      SAXReader reader = new SAXReader();
      try {
       org.dom4j.Document document = reader.read(new File("f:\\books.xml"));
       org.dom4j.Element bookStore = document.getRootElement();
       List<org.dom4j.Element> bookEles = bookStore.elements();
       for (org.dom4j.Element book : bookEles) {
        Book bookEntity = new Book();
        List<org.dom4j.Attribute> bookAttrs = book.attributes();
        for (org.dom4j.Attribute attr : bookAttrs) {
         if (attr.getName().equals("id")) {
          bookEntity.setId(attr.getValue());
         }
        }
        List<org.dom4j.Element> bookss = book.elements();
        for (org.dom4j.Element bookChild : bookss) {
         String name = bookChild.getName();
         String value = bookChild.getStringValue();
         if (name.equals("name")) {
          bookEntity.setName(value);
         }
         else if (name.equals("author")) {
          bookEntity.setAuthor(value);
         }
         else if (name.equals("year")) {
          bookEntity.setYear(value);
         }
         else if (name.equals("price")) {
          bookEntity.setPrice(value);
         }
         else if (name.equals("language")) {
          bookEntity.setLanguage(value);
         }
        }
        booksList.add(bookEntity);
        bookEntity = null;
       }
      } catch (DOMException e) {
       e.printStackTrace();
      } catch (DocumentException e) {
    	// TODO Auto-generated catch block
    	e.printStackTrace();
    }
     }
     @Test
     public void testPerformance() throws Exception{
      System.out.println("性能测试:");
      //测试DOM的性能:
      long start = System.currentTimeMillis();
      domXmlParser();
      System.out.println("DOM:"+ (System.currentTimeMillis() - start) );
      //测试SAX的性能:
      start = System.currentTimeMillis();
      saxXmlParser();
      System.out.println("SAX:"+ (System.currentTimeMillis() - start) );
      //测试JDOM的性能:
      start = System.currentTimeMillis();
      jdomXmlParser();
      System.out.println("JDOM:"+ (System.currentTimeMillis() - start) );
      //测试DOM4J的性能:
      start = System.currentTimeMillis();
      dom4jXmlParser();
      System.out.println("DOM4J:"+ (System.currentTimeMillis() - start) );
     
     }
    }
    
    
    
    
    
    
    
    package net.paoyun.handler;
    
    import java.util.ArrayList;
    
    import javax.xml.namespace.QName;
    
    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
    
    import net.paoyun.entity.Book;
    
    public class SAXHanlder extends DefaultHandler {
    	// 要遍历数量的下标
    	// int index = 0;
    	String value = null;
    	Book book = null;
    	ArrayList<Book> bookList = new ArrayList<Book>();
    
    	public ArrayList<Book> getBookList() {
    		return bookList;
    	}
    
    	/**
    	 * 用来遍历xml的开始标签。。可以获得属性名和属性值
    	 */
    	@Override
    	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    		// TODO Auto-generated method stub
    
    		// 调用父类的startElement
    		super.startElement(uri, localName, qName, attributes);
    		// 自定义属性的遍历
    		if (qName.equals("book")) {
    			// index++;
    			// 已知标签的属性名称,已知属性名称获取属性值
    			/*
    			 * String value = attributes.getValue("id");
    			 * System.out.println("属性值:"+value);
    			 */
    			book = new Book();
    			// 不知道属性的名称和个数
    			int num = attributes.getLength();
    			for (int i = 0; i < num; i++) {
    				// 有几个属性名就循环几次 得到属性名
    				System.out.println(attributes.getQName(i));
    
    				// 获取属性值,通过index的下标获取
    				System.out.println(attributes.getValue(i));
    				if (attributes.getQName(i).equals("id")) {
    					book.setId(attributes.getValue(i));
    				}
    			}
    		} else if (!qName.equals("book") && !qName.equals("bookstore")) {
    			System.out.println("节点名是:" + qName);
    
    		}
    
    	}
    
    	/**
    	 * 用来遍历xml的结束标签
    	 */
    
    	@Override
    	public void endElement(String uri, String localName, String qName) throws SAXException {
    		// TODO Auto-generated method stub
    		super.endElement(uri, localName, qName);
    		// 是否针对遍历的标签是否结束
    		if (qName.equals("book")) {
    			bookList.add(book);
    			book = null;
    
    		} else if (qName.equals("id")) {
    			book.setId(value);
    		} else if (qName.equals("name")) {
    			book.setName(value);
    		} else if (qName.equals("author")) {
    			book.setAuthor(value);
    		} else if (qName.equals("year")) {
    			book.setYear(value);
    		} else if (qName.equals("price")) {
    			book.setPrice(value);
    		} else if (qName.equals("qitq")) {
    			book.setQitq(value);
    		} else if (qName.equals("language")) {
    			book.setLanguage(value);
    		}
    
    	}
    
    	/**
    	 * 用来标志解析开始
    	 */
    	@Override
    	public void startDocument() throws SAXException {
    		// TODO Auto-generated method stub
    		System.out.println("解析开始");
    		super.startDocument();
    	}
    
    	/**
    	 * 用来标记解析结束
    	 */
    	@Override
    	public void endDocument() throws SAXException {
    		// TODO Auto-generated method stub
    		System.out.println("解析结束");
    		super.endDocument();
    
    	}
    
    	/**
    	 * 获取节点值中的内容
    	 */
    	@Override
    	public void characters(char[] ch, int start, int length) throws SAXException {
    		// TODO Auto-generated method stub
    		super.characters(ch, start, length);
    		// 得到节点值的内容
    		value = new String(ch, start, length);
    		// 去掉空格和多余的换行
    		if (!value.trim().equals("")) {
    			System.out.println("节点值是:" + value);
    		}
    
    	}
    }



    package net.paoyun.entity;


    public class Book {

    // 对应的book的节点名:

    private String id;

    private String name;

    private String author;

    private String year;

    private String price;

    private String qitq;

    private String language;


    public String getId() {

    return id;

    }


    public void setId(String id) {

    this.id = id;

    }


    public String getName() {

    return name;

    }


    public void setName(String name) {

    this.name = name;

    }


    public String getAuthor() {

    return author;

    }


    public void setAuthor(String author) {

    this.author = author;

    }


    public String getYear() {

    return year;

    }


    public void setYear(String year) {

    this.year = year;

    }


    public String getPrice() {

    return price;

    }


    public void setPrice(String price) {

    this.price = price;

    }


    public String getQitq() {

    return qitq;

    }


    public void setQitq(String qitq) {

    this.qitq = qitq;

    }


    public String getLanguage() {

    return language;

    }


    public void setLanguage(String language) {

    this.language = language;

    }


    @Override

    public String toString() {

    return "Book [id=" + id + ", name=" + name + ", author=" + author + ", year=" + year + ", price=" + price

    + ", qitq=" + qitq + ", language=" + language + "]";

    }


    }

      把需要用到的jar文件都要导入

    2回答·962浏览
  • 慕莱坞3979215 2016-08-23
    请问这样的项目需要怎么做

    没听明白

    1回答·710浏览
  • wshyzx 2016-08-19
    最后的DOM4J ?

    原理应该差不多把 一个用的迭代器Iterator 一个直接用的List  本质都是对子Element元素进行遍历,不必太纠结口误,学到东西明白了就好

    1回答·832浏览
  • 精慕门7312623 2016-05-16
    什么我的@Test报错还得加 import org.junit.Test

    因为Test也是要导入的啊

    1回答·4598浏览
  • Zoya 2016-04-23
    如何在一个类中导入两个相同名字的类型

    这就是package的作用了,你的document 是在不同的包里的,你把包名带上就可以了。例如老师的例子:org.jdom2.Document

    2回答·1278浏览
  • Vleyong 2015-10-14
    老师你的代码我导入myeclipse每一次都是有一个红色叹号在项目上。里面的代码连String都报错

    已经解决,原因是导入项目的时候没有添加IDE自带的jre,所以系统提示找不到

    1回答·1046浏览
  • 李阿昀 2015-07-26
    项目运行时报错?

    不支持的类版本错误,版本太低了吧,可能是哪个Jar包不支持

    1回答·1569浏览
  • leon33 2015-04-15
    要处理一个60M的xml文件,选择哪一种解析方法?
    sax或者jdom都可以
    1回答·566浏览
  • 隔壁王爷爷 2015-03-31
    在这次代码中JUnit起到了什么作用?
    已采纳 Crazy丶魂 的回答

    单元测试:快速的测试单个方法,否则你需要在main中来执行类的方法

    2回答·1299浏览
  • 疯狂的潜水员 2014-10-31
    2回答·1182浏览
数据加载中...
开始学习 免费