扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
work contact-info Location Shanghai-shuion-333 Postal 200020 Tel fix 63262299 mobile 1581344454 Appellation Mr. Wang |
清单 7 为这个信息文件构造一个 DOM 树,并将其作为 transformer 的输入。
清单 7. 构造 DOM 树
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.sax.SAXResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; class ContentHandler extends DefaultHandler { @Override public void characters(char[] ch, int start, int length) throws SAXException { String name = new String(ch, start, length); System.out.print(name + "\t"); } } public class DOMTest { /** * @param args * @throws TransformerException */ public static void main(String[] args) { try { DocumentBuilderFactory documentfactory = DocumentBuilderFactory .newInstance(); DocumentBuilder builder = documentfactory.newDocumentBuilder(); Document document = builder.newDocument(); Element root = document.createElement("work-contact-info"); Element loca = document.createElement("Location"); loca.setTextContent("Shanghai-shuion-333"); root.appendChild(loca); Element postal = document.createElement("Postal"); postal.setTextContent("200020"); root.appendChild(postal); Element tel = document.createElement("Tel"); root.appendChild(tel); Element fix = document.createElement("fix"); fix.setTextContent("63262299"); tel.appendChild(fix); Element mobile = document.createElement("mobile"); mobile.setTextContent("1581344454"); tel.appendChild(mobile); Element appellation = document.createElement("Appellation"); appellation.setTextContent("Mr. Wang"); root.appendChild(appellation); document.appendChild(root); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer; transformer = tFactory.newTransformer(); SAXResult result = new SAXResult(); ContentHandler cHandler = new ContentHandler(); result.setHandler(cHandler); transformer.transform(new DOMSource(document), result); } catch (Exception e) { e.printStackTrace(); } } } |
清单 8. 度量 XML 解析框架的运行时间
public class StAXTest { public static void main(String[] args) { final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<work-contact-info>" + "<Location>Shanghai-shuion-333</Location>" + "<Postal>200020</Postal>" + "<Tel><fix>63262299</fix>" + "<mobile>1581344454</mobile></Tel>" + "<Appellation>Mr. Wang</Appellation>" + "</work-contact-info>"; for (int i = 0; i < 10000; i++) { StAX(xml); } for (int i = 0; i < 10000; i++) { SAX(xml); } for (int i = 0; i < 10000; i++) { DOM(xml); } long current = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { StAX(xml); } current = System.currentTimeMillis() - current; System.out.println(current); current = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { SAX(xml); } current = System.currentTimeMillis() - current; System.out.println(current); current = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { DOM(xml); } current = System.currentTimeMillis() - current; System.out.println(current); } private static void StAX(final String xml) { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); InputStream input; try { input = new ByteArrayInputStream(xml.getBytes()); XMLEventReader xmlEventReader = inputFactory .createXMLEventReader(input); while (xmlEventReader.hasNext()) { XMLEvent event = xmlEventReader.nextEvent(); if (event.isStartElement()) { StartElement startElement = event.asStartElement(); } if (event.isCharacters()) { Characters text = event.asCharacters(); if (!text.isWhiteSpace()) { } } } } catch (XMLStreamException e) { e.printStackTrace(); } } private static void SAX(final String xml) { SAXParserFactory f = SAXParserFactory.newInstance(); InputStream input; try { SAXParser p = f.newSAXParser(); input = new ByteArrayInputStream(xml.getBytes()); p.parse(input, new DefaultHandler()); } catch (Exception e) { e.printStackTrace(); } } private static void DOM(final String xml) { DocumentBuilderFactory f = DocumentBuilderFactory.newInstance(); InputStream input; try { DocumentBuilder p = f.newDocumentBuilder(); input = new ByteArrayInputStream(xml.getBytes()); p.parse(input); } catch (Exception e) { e.printStackTrace(); } } } |
得出的数据如下:
2734 4953 6516 |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者