JavaXML解析的四种方法(连载)
阅读原文时间:2023年07月10日阅读:1

1. xml简介

  XML:指可扩展标记语言, Extensible Markup Language;类似HTML。XML的设计宗旨是传输数据,而非显示数据。

  一个xml文档实例:

1
2 3 4 许刚 5 工程师A 6 7 8 申林 9 实习生A 10 11

  第一行是 XML 声明。它定义 XML 的版本 (1.0) 和所使用的编码.

  下一行描述文档的根元素:开始,该根元素具有2个属性“name”,"address"。

  最后一行定义根元素的结尾。。

  · XML 文档形成一种树结构

  · XML 文档必须包含根元素。该元素是所有其他元素的父元素。

  · XML 文档中的元素形成了一棵文档树。这棵树从根部开始,并扩展到树的最底端。

所有元素均可拥有子元素:

1 2 3 4 5 ….. 6 7 8 9

  父、子以及同胞等术语用于描述元素之间的关系。父元素拥有子元素。相同层级上的子元素成为同胞(兄弟或姐妹)。

  节点:XML文档中的所有节点组成了一个文档树(或节点树)。XML文档中的每个元素、属性、文本等都代表着树中的一个节点。树起始于文档节点,并由此继续伸出枝条,直到处于这棵树最低级别的所有文本节点为止,常用节点类型如下表所示:

节点类型

附加说明

实例

元素节点(Element)

XML标记元素

属性节点(Attribute)

XML标记元素的属性

name=”Tencent”

文本节点(Text)

包括在XML标记中的文本段

工程师A

文档类型节点(DocumentType)

文档类型声明

﹤!DOCTYPE…﹥

注释节点Comment

XmlComment类注释节点。

(1) 节点关系

  通过上面的XML文档,我们构建出如下树状文档对象模型:

  

1
2 3 4 Everyday Italian 5 Giada De Laurentiis 6 2005 7 30.00 8 9 10 Harry Potter 11 J K. Rowling 12 2005 13 29.99 14 15 16 Learning XML 17 Erik T. Ray 18 2003 19 39.95 20 21

  再如上例xml文档:

  例子中的根元素是 。文档中的所有 元素都被包含在 中。 元素有 4 个子元素:、< author>、<year>、<price>。</p> <h3 id="(2)xml的特点">(2)xml的特点</h3> <p>  a). XML 的属性值须加引号。</p> <p>  · 与 HTML 类似,XML 也可拥有属性(名称/值的对)。</p> <p>  · 在 XML 中,XML 的属性值须加引号。</p> <p>  b). XML 文档必须有根元素.XML 文档必须有一个元素是所有其他元素的<strong>父元素</strong>。该元素称为<strong>根元素</strong>。</p> <p>  c). XML 标签对大小写敏感.</p> <p>  · XML 元素使用 XML 标签进行定义。</p> <p>  · XML 标签对大小写敏感。在 XML 中,标签 <Letter> 与标签 <letter> 是不同的。必须使用相同的大小写来编写打开标签和关闭标签。</p> <p>  d). XML 中的注释语法: </p> <!-- This is a comment --> <p>  e). XML 元素 vs. 属性</p> <p><img class='lazyload' data-src="https://article.cdnof.com/2307/4b79a7ab-6d9b-4512-891b-0f7ac92041e3.gif" alt="" /></p> <p>1 <person sex="female"> 2 <firstname>Anna</firstname> 3 <lastname>Smith</lastname> 4 </person> <br /> 5 <br /> 6 <person> 7 <sex>female</sex> 8 <firstname>Anna</firstname> 9 <lastname>Smith</lastname> 10 </person> </p> <p><img class='lazyload' data-src="https://article.cdnof.com/2307/a9f71d11-a6d1-4860-a6e7-e8bfc886bf99.gif" alt="" /></p> <p>  在第一行中,sex 是一个属性。在第7行中,sex 则是一个子元素。两个例子均可提供相同的信息。</p> <h1 id="2-xml-解析">2. xml 解析</h1> <p>  xml解析方法有四种:  </p> <p>  · DOM(JAXP Crimson 解析器):W3C为HTML和XML分析器制定的标准接口规范,基于树,可随机动态访问和更新文档的内容、结构、样式。</p> <p>  · <a rel="nofollow noopener noreferrer" href="http://sax.sourceforge.net/">SAX</a>(simple API for XML):不是W3C的标准,而是由XML-DEV邮件列表成员于1998年为Java语言开发的一种基于事件的简单API。基于事件,逐行解析,顺序访问XML文档,速度快,处理功能简单。</p> <p>   · <a rel="nofollow noopener noreferrer" href="http://www.jdom.org/">JDOM</a>:鉴于DOM的低效率,而SAX又不能随机处理XML文档,Jason Hunter与Brett McLaughlin于2000年春天,开始创建一种能充分体现两者优势的<a rel="nofollow noopener noreferrer" href="http://sourceforge.net/projects/dom4j">API</a>——<a rel="nofollow noopener noreferrer" href="http://www.jdom.org/">JDOM</a>(Java-based DOM,基于Java的DOM),它是一个基于Java的对象模型,树状结构,能使读取、操作和写入XML文档,比DOM更高效,比SAX更强大,但由于使用了大量的类而不使用接口导致灵活性降低。</p> <p>  <a rel="nofollow noopener noreferrer" href="http://sourceforge.net/projects/dom4j">· DOM4J</a>:DOM4J是一个易用的,开源的库,用于XML,XPath,XSLT。它应用于Java平台,采用了Java集合框架并完全支持DOM,SAX,JAXP。它提供了大量的接口,因此比JDOM更具有灵活性。</p> <p>  DOM(Document Object Model文档对象模型),是W3C为HTML和XML分析器制定的标准接口规范。 </p> <p><strong>特点:</strong>独立于语言,跨平台(可以在各种编程和脚本语言中使用),需要将整个文档读入内存,在内存中创建文档树,可随即访问文档中的特定节点,对内存的要求比较高,经过测试,访问速度相对于其他解析方式较慢,适用于简单文档的随即处理。</p> <p>常用的节点属性:</p> <p><strong>属性</strong></p> <p><strong>描述</strong></p> <p>nodeName</p> <p>结点名称</p> <p>nodeValue</p> <p>结点内部值,通常只应用于文本结点</p> <p>nodeType</p> <p>节点类型对应的数字</p> <p>parentNode</p> <p>如果存在,指向当前结点的父亲结点</p> <p>childNodes</p> <p>子结点列表</p> <p>firstChild</p> <p>如果存在,指向当前元素的第一个子结点</p> <p>lastChild</p> <p>如果存在,指向当前元素的最后一个子结点</p> <p>previousSibling</p> <p>指向当前结点的前一个兄弟结点</p> <p>nextSibling</p> <p>指向当前结点的后一个兄弟结点</p> <p>attributes</p> <p>元素的属性列表</p> <p>常用的节点方法:</p> <p><strong>操作类型</strong></p> <p><strong>方法原型</strong></p> <p><strong>描述</strong></p> <p>访问节点</p> <p>getElementById(id)</p> <p>根据ID属性查找元素节点</p> <p>getElementsByName(name)</p> <p>根据name属性查找元素集</p> <p>getElementsByTagName(tagName)</p> <p>根据元素标记名称查找元素集</p> <p>创建节点</p> <p>createElement(tagName)</p> <p>创建元素节点</p> <p>createTestNode(string)</p> <p>创建文本节点</p> <p>createAttribute(name)</p> <p>创建属性节点</p> <p>插入和添加节点</p> <p>appendChild(newChild)</p> <p>添加子节点到目标节点上</p> <p>insertBefore(newChild,targetChild)</p> <p>将newChild节点插入到targetChild节点之前</p> <p>复制节点</p> <p>CloneNode(bool)</p> <p>复制该节点,由bool确定是否复制子节点</p> <p>删除和替换节点</p> <p>removeChild(childName)</p> <p>删除由childName指定的节点</p> <p>replaceChild(newChild,oldChild)</p> <p>用newChild替换oldChild</p> <p>属性节点操作</p> <p>getAttribute(name)</p> <p>返回目标对象指定属性名称为name的属性值</p> <p>setAttribute(name,value)</p> <p>修改目标节点指定属性名称为name的属性值为value</p> <p>removeAttribute(name)</p> <p>删除目标节点指定属性名称为name的属性</p> <p>(1)读取本地xml文档解析为对象的步骤: </p> <p>  * 首先利用DocumentBuilderFactory创建一个DocumentBuilderFactory实例;然后利用DocumentBuilderFactory创建DocumentBuilder</p> <p>//创建DocumentBuilderFactory工厂实例。 <br /> DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance(); <br /> //通过文档创建工厂创建文档创建器 <br /> DocumentBuilder dBuilder = dbfactory.newDocumentBuilder();</p> <p>  * 然后加载XML文档(Document) <strong>:</strong> 通过文档创建器DocumentBuilder的parse方法解析参数URL指定的XML文档,并返回一个Document 对象。</p> <p>Document doc = dBuilder.parse(url);</p> <p>  * 然后获取文档的根结点(Element), <br />   * 然后获取根结点中所有子节点的列表(NodeList), <br />   * 然后使用再获取子节点列表中的需要读取的结点。</p> <p><strong>在实际开发中多思考,灵活运用,非是难事儿!,对于程序员来讲,精彩的代码是如何想出来的,远比看到精彩的代码更加令人期望!</strong></p> <p><?xml version="1.0" encoding="UTF-8" standalone="no"?> <br /> <books> <book typeId="1"> <bookId>001</bookId> <bookName>Maven实战</bookName> <bookPrice>51</bookPrice> <bookAuthor>陈晓斌</bookAuthor> </book> <book typeId="2"> <bookId>002</bookId> <bookName>谈谈支付宝底层体系架构的大家</bookName> <bookPrice>151</bookPrice> <bookAuthor>程立</bookAuthor> </book> <book TypeId="3"> <bookId>003</bookId> <bookName>预约死亡</bookName> <bookPrice>99</bookPrice> <bookAuthor>毕淑敏</bookAuthor> </book> <book TypeId="3"> <bookId>003</bookId> <bookName>预约死亡</bookName> <bookPrice>56</bookPrice> <bookAuthor>毕淑敏</bookAuthor> </book> </books></p> <p>import javax.xml.parsers.DocumentBuilder; <br /> import javax.xml.parsers.DocumentBuilderFactory; <br /> import javax.xml.parsers.ParserConfigurationException; <br /> import javax.xml.transform.Result; <br /> import javax.xml.transform.Source; <br /> import javax.xml.transform.Transformer; <br /> import javax.xml.transform.TransformerFactory; <br /> import javax.xml.transform.dom.DOMSource; <br /> import javax.xml.transform.stream.StreamResult;</p> <p>import org.w3c.dom.Document; <br /> import org.w3c.dom.Element; <br /> import org.w3c.dom.Node; <br /> import org.w3c.dom.NodeList;</p> <p>public class Test { <br /> /** <br /> * XML读取 <br /> * <a rel="nofollow noopener noreferrer" href="https://github.com/throws">@throws</a> Exception <br /> */ <br /> public static void xml() throws Exception{ <br /> //1.构建一个工厂 <br /> DocumentBuilderFactory dbFactory =DocumentBuilderFactory.newInstance(); <br /> //2.构建builder1 <br /> DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder(); <br /> //3.Document对象 <br /> //一般放相对路径就可以了,程序自动在项目下寻址 <br /> Document document = dbBuilder.parse("books.xml"); <br /> //4.XML解析 <br /> NodeList list =document.getElementsByTagName("book"); <br /> for (int i = 0; i < list.getLength(); i++) { <br /> Element item = (Element)list.item(i); <br /> String attribute=item.getAttribute("typeId"); <br /> String bookId=item.getElementsByTagName("bookId").item(0).getTextContent(); <br /> String bookName=item.getElementsByTagName("bookName").item(0).getTextContent(); <br /> String bookPrice=item.getElementsByTagName("bookPrice").item(0).getTextContent(); <br /> String bookAuthor=item.getElementsByTagName("bookAuthor").item(0).getTextContent(); <br /> System.out.println(bookId); <br /> System.out.println(bookName); <br /> System.out.println(bookPrice); <br /> System.out.println(bookAuthor); <br /> //System.out.println(attribute); <br /> System.out.println("-----------------"); <br /> } <br /> } <br /> /** <br /> * XML添加 <br /> * <a rel="nofollow noopener noreferrer" href="https://github.com/throws">@throws</a> Exception <br /> */ <br /> public static void insert() throws Exception{ <br /> //1.构建一个工厂 <br /> DocumentBuilderFactory dbFactory =DocumentBuilderFactory.newInstance(); <br /> //2.构建builder1 <br /> DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder(); <br /> //3.Document对象 <br /> Document document = dbBuilder.parse("books.xml"); <br /> //1.伪造内存中的节点,游离节点 <br /> Element book = document.createElement("book"); <br /> book.setAttribute("TypeId", "3"); <br /> Element bookId=document.createElement("bookId"); <br /> bookId.setTextContent("003"); <br /> Element bookName = document.createElement("bookName"); <br /> bookName.setTextContent("预约死亡"); <br /> Element bookPrice = document.createElement("bookPrice"); <br /> bookPrice.setTextContent("56"); <br /> Element bookAuthor = document.createElement("bookAuthor"); <br /> bookAuthor.setTextContent("毕淑敏"); <br /> book.appendChild(bookId); <br /> book.appendChild(bookName); <br /> book.appendChild(bookPrice); <br /> book.appendChild(bookAuthor);</p> <pre><code> //1.将Book节点和整篇文档建立关联 document.getElementsByTagName("books").item(0).appendChild(book); //1.传输工厂 TransformerFactory transformerFactory = TransformerFactory.newInstance(); //2.传输器 Transformer transformer = transformerFactory.newTransformer(); Source source = new DOMSource(document); StreamResult result = new StreamResult("books.xml"); //3.传输方法 transformer.transform(source, result); System.out.println("save ok!"); } /\*\* \* XML修改 \* @throws Exception \*/ public static void update() throws Exception{ //1.构建一个工厂 DocumentBuilderFactory dbFactory =DocumentBuilderFactory.newInstance(); //2.构建builder1 DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder(); //3.Document对象 Document document = dbBuilder.parse("books.xml"); //修改指定item的书的价格 //"E:/eclispsespace/JavaOOP-Day-09-0100/books.xml" Element book = (Element)document.getElementsByTagName("book").item(2); book.getElementsByTagName("bookPrice").item(0).setTextContent("99"); //传输工程 TransformerFactory transformerFactory = TransformerFactory.newInstance(); //传输器 Transformer transformer =transformerFactory.newTransformer(); Source source = new DOMSource(document); Result result = new StreamResult("books.xml"); transformer.transform(source, result); System.out.println("修改完毕!"); } /\*\* \* XML删除 \* @throws Exception \*/ public static void delXml() throws Exception{ //1.构建一个工厂 DocumentBuilderFactory dbFactory =DocumentBuilderFactory.newInstance(); //2.构建builder1 DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder(); //3.Document对象 //一般放相对路径就可以了,程序自动在项目下寻址 Document document = dbBuilder.parse("books.xml"); Element book = (Element)document.getElementsByTagName("book").item(2); document.getElementsByTagName("books").item(0).removeChild(book); //传输工程 TransformerFactory transformerFactory = TransformerFactory.newInstance(); //传输器 Transformer transformer = transformerFactory.newTransformer(); Source source = new DOMSource(document); Result result =new StreamResult("books.xml"); transformer.transform(source, result); System.out.println("删除成功!"); } public static void main(String\[\] args) throws Exception { //xml();//读取 //insert();//增加 //update();//修改 delXml() ;//删除 }</code></pre> <p>}</p> <h3 id="dom4j:">Dom4j:</h3> <p>Dom4j是一个Java的XML API,是jdom的升级品,用来读写XML文件的。dom4j是一个十分优秀的JavaXML API,具有性能优异、功能强大和极其易使用的特点,它的性能超过sun公司官方的dom技术,同时它也是一个开放源代码的软件,可以在SourceForge上找到它。在IBM developerWorks上面还可以找到一篇文章,对主流的Java XML API进行的性能、功能和易用性的评测,所以可以知道dom4j无论在哪个方面都是非常出色的。如今可以看到越来越多的Java软件都在使用dom4j来读写XML,特别值得一提的是连Sun的JAXM也在用dom4j。这已经是必须使用的jar包,Hibernate也用它来读写文件。</p> <p>所需jar包:dom4j-1.6.1.jar</p> <h3 id="实例图:">实例图:</h3> <p><img class='lazyload' data-src="https://article.cdnof.com/2307/e2a69ebc-a945-47e8-9525-daa895383c29.png" alt="" /></p> <h3 id="xml文件:">XML文件:</h3> <p><?xml version="1.0" encoding="UTF-8"?> <br /> <people city="beijing"> <student name="milton" age="22"></student> <student name="lego" age="23"></student> <teacher name="bruce" age="27"></teacher> <teacher name="ziven" age="29"></teacher> </people></p> <h3 id="对应entity:">对应entity:</h3> <h3 id="people类:">People类:</h3> <p><img class='lazyload' data-src="https://article.cdnof.com/2307/270e2f57-2acd-4483-b3fd-d87ae4604fb9.png" alt="" /></p> <h3 id="student类:">Student类:</h3> <p><img class='lazyload' data-src="https://article.cdnof.com/2307/5fc0d719-e7aa-4a52-9db8-d5158cfb294a.png" alt="" /></p> <h3 id="teacher类:">Teacher类:</h3> <p><img class='lazyload' data-src="https://article.cdnof.com/2307/e51d3dcd-78c6-4b40-b735-78d23c45802b.png" alt="" /></p> <h3 id="testxml类">TestXml类:</h3> <p>package cn.resolve.dom4j; <br /> import java.io.File; <br /> import java.util.ArrayList; <br /> import java.util.Iterator; <br /> import java.util.List; <br /> import org.dom4j.Document; <br /> import org.dom4j.DocumentException; <br /> import org.dom4j.Element; <br /> import org.dom4j.io.SAXReader; <br /> import cn.resolve.entity.People; <br /> import cn.resolve.entity.Student; <br /> import cn.resolve.entity.Teacher; <br /> /** <br /> * 测试使用dom4j解析XML <br /> * <a rel="nofollow noopener noreferrer" href="https://github.com/author">@author</a> Zhang <br /> * <br /> */ <br /> public class TestXml { <br /> public static People xmlParse(String xmlPath){ <br /> File xmlFile = new File(xmlPath); <br /> System.out.println(xmlFile.getPath()); <br /> //判断文件是否存在 <br /> if (xmlFile.exists()) { <br /> SAXReader reader = new SAXReader(); <br /> People people = new People(); <br /> try { <br /> //读入文档流 <br /> Document document = reader.read(xmlFile); <br /> //获取根节点 <br /> Element root =document.getRootElement(); <br /> //创建两个对象集合 <br /> List<Student> students = new ArrayList<Student>(); <br /> List<Teacher> teachers = new ArrayList<Teacher>(); <br /> //拿到attribute属性的值 <br /> people.setCity(root.attributeValue("city")); <br /> //解析student节点 <br /> for (Iterator iterator = root.elementIterator("student");iterator.hasNext();) { <br /> Element eStudent= (Element)iterator.next(); <br /> Student student = new Student(eStudent.attributeValue("name"),Integer.parseInt(eStudent.attributeValue("age"))); <br /> //添加到集合 <br /> students.add(student); <br /> } <br /> //解析teacher节点 <br /> for (Iterator iterator= root.elementIterator("teacher");iterator.hasNext();) { <br /> Element eTeacher = (Element)iterator.next(); <br /> Teacher teacher = new Teacher(eTeacher.attributeValue("name"),Integer.parseInt(eTeacher.attributeValue("age"))); <br /> teachers.add(teacher); <br /> } <br /> people.setStudent(students); <br /> people.setTeacher(teachers); <br /> System.out.println("success!"); <br /> } catch (DocumentException e) { <br /> e.printStackTrace(); <br /> } <br /> return people; <br /> }else { <br /> System.out.println("file is not exist!"); <br /> return null; <br /> } <br /> } <br /> public static void main(String[] args) { <br /> String xmlpath = "src/people.xml"; <br /> //解析people.xml <br /> People people =xmlParse(xmlpath); <br /> System.out.println("file full path is xmlpath "); <br /> List<Teacher> teachers = new ArrayList<Teacher>(); <br /> List<Student> students = new ArrayList<Student>(); <br /> System.out.println(people.getCity()); <br /> //处理解析结果 <br /> for (Student s : students) { <br /> System.out.println("学生姓名/t/t年龄"); <br /> System.out.println(s.getName()+":"+s.getAge()); <br /> } <br /> for (Teacher t: teachers) { <br /> System.out.println("教师姓名/t/t年龄"); <br /> System.out.println(); <br /> } <br /> } <br /> }</p></div></div><div class="MuiGrid-root jss8 MuiGrid-item MuiGrid-grid-xs-true MuiGrid-grid-md-3"><div class="MuiTypography-root jss26 MuiTypography-body1"><div class="MuiTypography-root jss27 MuiTypography-body1"><canvas style="height:108px;width:108px" height="108" width="108"></canvas><div class="MuiTypography-root jss28 MuiTypography-body1"><p class="MuiTypography-root jss29 MuiTypography-body1">手机扫一扫</p><p class="MuiTypography-root jss29 MuiTypography-body1">移动阅读更方便</p></div></div></div><div class="MuiTypography-root jss9 MuiTypography-body1"><div class="MuiTypography-root jss30 MuiTypography-body1" style="height:150px"><div class="swiper-container jss32"><div class="swiper-pagination"></div><div class="swiper-wrapper"><div class="swiper-slide jss32"><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover jss32 MuiTypography-colorInherit" target="_blank" rel="nofollow noopener noreferrer" href="https://qd.rs/aliyun"><img alt="阿里云服务器" class="jss31" src="https://article.cdnof.com/promotion/aliyun.jpg"/></a></div><div class="swiper-slide jss32"><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover jss32 MuiTypography-colorInherit" target="_blank" rel="nofollow noopener noreferrer" href="https://qd.rs/tencent"><img alt="腾讯云服务器" class="jss31" src="https://article.cdnof.com/promotion/tencent.jpg"/></a></div><div class="swiper-slide jss32"><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover jss32 MuiTypography-colorInherit" target="_blank" rel="nofollow noopener noreferrer" href="https://qd.rs/qiniu"><img alt="七牛云服务器" class="jss31" src="https://article.cdnof.com/promotion/qiniu.png"/></a></div></div></div></div></div><div class="MuiTypography-root MuiTypography-body1"><div class="MuiTypography-root jss33 MuiTypography-body1"><p class="MuiTypography-root jss34 MuiTypography-body1">你可能感兴趣的文章</p><div class="MuiList-root MuiList-padding" aria-label="main mailbox folders"><div class="MuiTypography-root MuiTypography-body1"><div class="MuiButtonBase-root MuiListItem-root jss37 MuiListItem-gutters MuiListItem-button" tabindex="0" role="button" aria-disabled="false"><div class="MuiListItemIcon-root jss36"><svg class="MuiSvgIcon-root MuiSvgIcon-colorError" focusable="false" viewBox="0 0 24 24" aria-hidden="true"><path d="M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-5 14h-2V9h-2V7h4v10z"></path></svg></div><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover jss35 MuiTypography-colorInherit" target="_blank" href="https://v2as.com/article/1f20385e-eaa9-4f43-b079-64c430020b86">ASP.NET 网站管理工具</a></div><hr class="MuiDivider-root"/></div><div class="MuiTypography-root MuiTypography-body1"><div class="MuiButtonBase-root MuiListItem-root jss37 MuiListItem-gutters MuiListItem-button" tabindex="0" role="button" aria-disabled="false"><div class="MuiListItemIcon-root jss36"><svg class="MuiSvgIcon-root" focusable="false" viewBox="0 0 24 24" aria-hidden="true"><path d="M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-4 8c0 1.11-.9 2-2 2h-2v2h4v2H9v-4c0-1.11.9-2 2-2h2V9H9V7h4c1.1 0 2 .89 2 2v2z"></path></svg></div><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover jss35 MuiTypography-colorInherit" target="_blank" href="https://v2as.com/article/71ae3035-d6b6-4775-aaab-7e12f232edb9">Redis进阶实践之十九 Redis如何使用lua脚本</a></div><hr class="MuiDivider-root"/></div><div class="MuiTypography-root MuiTypography-body1"><div class="MuiButtonBase-root MuiListItem-root jss37 MuiListItem-gutters MuiListItem-button" tabindex="0" role="button" aria-disabled="false"><div class="MuiListItemIcon-root jss36"><svg class="MuiSvgIcon-root MuiSvgIcon-colorSecondary" focusable="false" viewBox="0 0 24 24" aria-hidden="true"><path d="M19.01 3h-14c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-4 7.5c0 .83-.67 1.5-1.5 1.5.83 0 1.5.67 1.5 1.5V15c0 1.11-.9 2-2 2h-4v-2h4v-2h-2v-2h2V9h-4V7h4c1.1 0 2 .89 2 2v1.5z"></path></svg></div><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover jss35 MuiTypography-colorInherit" target="_blank" href="https://v2as.com/article/5240ba5c-dcee-481a-b9b2-b83bd88a5710">log4j2同步日志引发的性能问题</a></div><hr class="MuiDivider-root"/></div><div class="MuiTypography-root MuiTypography-body1"><div class="MuiButtonBase-root MuiListItem-root jss37 MuiListItem-gutters MuiListItem-button" tabindex="0" role="button" aria-disabled="false"><div class="MuiListItemIcon-root jss36"><p class="MuiTypography-root MuiTypography-body1">4</p></div><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover jss35 MuiTypography-colorInherit" target="_blank" href="https://v2as.com/article/aacfe9d2-c6a0-4081-8032-418604f6a6b5">hadoop集群搭建及编程实践</a></div><hr class="MuiDivider-root"/></div><div class="MuiTypography-root MuiTypography-body1"><div class="MuiButtonBase-root MuiListItem-root jss37 MuiListItem-gutters MuiListItem-button" tabindex="0" role="button" aria-disabled="false"><div class="MuiListItemIcon-root jss36"><p class="MuiTypography-root MuiTypography-body1">5</p></div><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover jss35 MuiTypography-colorInherit" target="_blank" href="https://v2as.com/article/7c4258c6-9fe1-4560-b3b7-5eacb6a5872d">一键安装lnmp 环境</a></div><hr class="MuiDivider-root"/></div><div class="MuiTypography-root MuiTypography-body1"><div class="MuiButtonBase-root MuiListItem-root jss37 MuiListItem-gutters MuiListItem-button" tabindex="0" role="button" aria-disabled="false"><div class="MuiListItemIcon-root jss36"><p class="MuiTypography-root MuiTypography-body1">6</p></div><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover jss35 MuiTypography-colorInherit" target="_blank" href="https://v2as.com/article/ba2ff4a0-6b69-421b-b077-3a554853f5a6">提高 Web 开发效率的10个VS Code扩展插件,你知道吗?</a></div><hr class="MuiDivider-root"/></div><div class="MuiTypography-root MuiTypography-body1"><div class="MuiButtonBase-root MuiListItem-root jss37 MuiListItem-gutters MuiListItem-button" tabindex="0" role="button" aria-disabled="false"><div class="MuiListItemIcon-root jss36"><p class="MuiTypography-root MuiTypography-body1">7</p></div><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover jss35 MuiTypography-colorInherit" target="_blank" href="https://v2as.com/article/09a74eb1-ca7a-43ae-b13e-92020f285c7b">openNebula集群搭建</a></div><hr class="MuiDivider-root"/></div><div class="MuiTypography-root MuiTypography-body1"><div class="MuiButtonBase-root MuiListItem-root jss37 MuiListItem-gutters MuiListItem-button" tabindex="0" role="button" aria-disabled="false"><div class="MuiListItemIcon-root jss36"><p class="MuiTypography-root MuiTypography-body1">8</p></div><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover jss35 MuiTypography-colorInherit" target="_blank" href="https://v2as.com/article/70990184-a0be-4d44-b745-fed63d7f36cf">cephadm快速部署指定版本ceph集群及生产问题处理</a></div><hr class="MuiDivider-root"/></div><div class="MuiTypography-root MuiTypography-body1"><div class="MuiButtonBase-root MuiListItem-root jss37 MuiListItem-gutters MuiListItem-button" tabindex="0" role="button" aria-disabled="false"><div class="MuiListItemIcon-root jss36"><p class="MuiTypography-root MuiTypography-body1">9</p></div><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover jss35 MuiTypography-colorInherit" target="_blank" href="https://v2as.com/article/187554b2-7be5-4da3-923a-283107e20136">C# - XMLHelper :一个操作XML的简单类库</a></div><hr class="MuiDivider-root"/></div><div class="MuiTypography-root MuiTypography-body1"><div class="MuiButtonBase-root MuiListItem-root jss37 MuiListItem-gutters MuiListItem-button" tabindex="0" role="button" aria-disabled="false"><div class="MuiListItemIcon-root jss36"><p class="MuiTypography-root MuiTypography-body1">10</p></div><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover jss35 MuiTypography-colorInherit" target="_blank" href="https://v2as.com/article/a965f5c3-f0a7-477a-b2d6-46ef08d3b3f3">MQTT vs. XMPP,哪一个才是IoT通讯协议的正解</a></div></div></div></div></div></div></div></div><footer style="margin-top:30px"><p class="MuiTypography-root MuiTypography-body2 MuiTypography-colorTextSecondary MuiTypography-alignCenter">Copyright © <a class="MuiTypography-root MuiLink-root MuiLink-underlineHover MuiTypography-colorInherit" href="https://v2as.com" title="哇哦,有大量工具等你探索">V2AS | 问路</a> <!-- -->2024<!-- --> <!-- -->.</p><p class="MuiTypography-root MuiTypography-body2 MuiTypography-colorTextSecondary MuiTypography-alignCenter"><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover MuiTypography-colorInherit" rel="nofollow noopener noreferrer" href="https://beian.miit.gov.cn/">浙ICP备15029886号</a></p></footer></div></div><script id="__NEXT_DATA__" type="application/json">{"props":{"pageProps":{"article":{"article_id":"4b01e1ed-2481-41f1-8738-60b50a17caed","title":"JavaXML解析的四种方法(连载)","link":"","description":"1. xml简介\n  XML:指可扩展标记语言, Extensible Markup Language;类似HTML。XML的设计宗旨是传输数据,而非显示数据。\n  一个xml文档实例:\n\n\n 1 \u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\n 2 \u003ccompany name=\"Tencent\" address=\"深圳市南山区\"\u003e\n 3 \u003cdepartment ","image":"https://article.cdnof.com/2307/1630751f-933e-422b-a1de-f206ac7afa46.gif","keywords":["XML","xml","节点","文档","book","解析","import","元素","document","Element"],"created_at":"2023-07-10T12:13:01.326Z","html":"\u003ch1 id=\"1-xml简介\"\u003e1. xml简介\u003c/h1\u003e\n\u003cp\u003e  XML:指可扩展标记语言, Extensible Markup Language;类似HTML。XML的设计宗旨是传输数据,而非显示数据。\u003c/p\u003e\n\u003cp\u003e  一个xml文档实例:\u003c/p\u003e\n\u003cp\u003e\u003cimg class='lazyload' data-src=\"https://article.cdnof.com/2307/1630751f-933e-422b-a1de-f206ac7afa46.gif\" alt=\"\" /\u003e\u003c/p\u003e\n\u003cp\u003e1 \u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e \u003cbr /\u003e\n 2 \u003ccompany name=\"Tencent\" address=\"深圳市南山区\"\u003e \n 3 \u003cdepartment deptNo=\"001\" name=\"development\"\u003e \n 4 \u003cemployee id=\"devHead\" position=\"minister\"\u003e许刚\u003c/employee\u003e \n 5 \u003cemployee position=\"developer\"\u003e工程师A\u003c/employee\u003e \n 6 \u003c/department\u003e \n 7 \u003cdepartment deptNo=\"002\" name=\"education\"\u003e \n 8 \u003cemployee position=\"minister\" telephone=\"1234567\"\u003e申林\u003c/employee\u003e \n 9 \u003cemployee position=\"trainee\"\u003e实习生A\u003c/employee\u003e \n10 \u003c/department\u003e \n11 \u003c/company\u003e\u003c/p\u003e\n\u003cp\u003e\u003cimg class='lazyload' data-src=\"https://article.cdnof.com/2307/e0973632-d96f-4dd3-bf28-638b06a69163.gif\" alt=\"\" /\u003e\u003c/p\u003e\n\u003cp\u003e  第一行是\u0026nbsp;XML 声明。它定义 XML 的版本 (1.0) 和所使用的编码.\u003c/p\u003e\n\u003cp\u003e  下一行描述文档的根元素:\u003ccompany\u003e开始,该根元素具有2个属性“name”,\"address\"。\u003c/p\u003e\n\u003cp\u003e  最后一行定义根元素的结尾。\u003c/company\u003e。\u003c/p\u003e\n\u003cp\u003e  ·\u0026nbsp;XML 文档形成一种树结构\u003c/p\u003e\n\u003cp\u003e  ·\u0026nbsp;XML 文档必须包含根元素。该元素是所有其他元素的父元素。\u003c/p\u003e\n\u003cp\u003e  ·\u0026nbsp;XML 文档中的元素形成了一棵文档树。这棵树从根部开始,并扩展到树的最底端。\u003c/p\u003e\n\u003cp\u003e所有元素均可拥有子元素:\u003c/p\u003e\n\u003cp\u003e\u003cimg class='lazyload' data-src=\"https://article.cdnof.com/2307/14e9d447-9a61-464e-ae34-90cd47539f68.gif\" alt=\"\" /\u003e\u003c/p\u003e\n\u003cp\u003e1 \u003croot\u003e \n2 \n3 \u003cchild\u003e \n4 \n5 \u003csubchild\u003e…..\u003c/subchild\u003e \n6 \n7 \u003c/child\u003e \n8 \n9 \u003c/root\u003e\u003c/p\u003e\n\u003cp\u003e\u003cimg class='lazyload' data-src=\"https://article.cdnof.com/2307/e906733b-2761-41c5-8f9c-01ebfe7ac533.gif\" alt=\"\" /\u003e\u003c/p\u003e\n\u003cp\u003e  父、子以及同胞等术语用于描述元素之间的关系。父元素拥有子元素。相同层级上的子元素成为同胞(兄弟或姐妹)。\u003c/p\u003e\n\u003cp\u003e  节点:XML文档中的所有节点组成了一个文档树(或节点树)。XML文档中的每个元素、属性、文本等都代表着树中的一个节点。树起始于文档节点,并由此继续伸出枝条,直到处于这棵树最低级别的所有文本节点为止,常用节点类型如下表所示:\u003c/p\u003e\n\u003cp\u003e节点类型\u003c/p\u003e\n\u003cp\u003e附加说明\u003c/p\u003e\n\u003cp\u003e实例\u003c/p\u003e\n\u003cp\u003e元素节点(Element)\u003c/p\u003e\n\u003cp\u003eXML标记元素\u003c/p\u003e\n\u003cp\u003e\u003ccompany\u003e…\u003c/company\u003e\u003c/p\u003e\n\u003cp\u003e属性节点(Attribute)\u003c/p\u003e\n\u003cp\u003eXML标记元素的属性\u003c/p\u003e\n\u003cp\u003ename=”Tencent”\u003c/p\u003e\n\u003cp\u003e文本节点(Text)\u003c/p\u003e\n\u003cp\u003e包括在XML标记中的文本段\u003c/p\u003e\n\u003cp\u003e工程师A\u003c/p\u003e\n\u003cp\u003e文档类型节点(DocumentType)\u003c/p\u003e\n\u003cp\u003e文档类型声明\u003c/p\u003e\n\u003cp\u003e﹤!DOCTYPE…﹥\u003c/p\u003e\n\u003cp\u003e注释节点Comment\u003c/p\u003e\n\u003cp\u003eXmlComment类注释节点。\u003c/p\u003e\n\u003cp\u003e\u003c!—文档注释-\u003e\u003c/p\u003e\n\u003ch3 id=\"1-节点关系\"\u003e(1) 节点关系\u003c/h3\u003e\n\u003cp\u003e  通过上面的XML文档,我们构建出如下树状文档对象模型:\u003c/p\u003e\n\u003cp\u003e  \u003cimg class='lazyload' data-src=\"https://article.cdnof.com/2307/324bb595-c8ee-4773-abac-c5bf145970c9.jpg\" alt=\"\" /\u003e\u003c/p\u003e\n\u003cp\u003e\u003cimg class='lazyload' data-src=\"https://article.cdnof.com/2307/086fbd46-e529-48f1-a54c-332c96641b30.gif\" alt=\"\" /\u003e\u003c/p\u003e\n\u003cp\u003e1 \u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e \u003cbr /\u003e\n 2 \u003cbookstore\u003e \n 3 \u003cbook category=\"COOKING\"\u003e \n 4 \u003ctitle lang=\"en\"\u003eEveryday Italian\u003c/title\u003e \n 5 \u003cauthor\u003eGiada De Laurentiis\u003c/author\u003e \n 6 \u003cyear\u003e2005\u003c/year\u003e \n 7 \u003cprice\u003e30.00\u003c/price\u003e \n 8 \u003c/book\u003e \n 9 \u003cbook category=\"CHILDREN\"\u003e \n10 \u003ctitle lang=\"en\"\u003eHarry Potter\u003c/title\u003e \n11 \u003cauthor\u003eJ K. Rowling\u003c/author\u003e \n12 \u003cyear\u003e2005\u003c/year\u003e \n13 \u003cprice\u003e29.99\u003c/price\u003e \n14 \u003c/book\u003e \n15 \u003cbook category=\"WEB\"\u003e \n16 \u003ctitle lang=\"en\"\u003eLearning XML\u003c/title\u003e \n17 \u003cauthor\u003eErik T. Ray\u003c/author\u003e \n18 \u003cyear\u003e2003\u003c/year\u003e \n19 \u003cprice\u003e39.95\u003c/price\u003e \n20 \u003c/book\u003e \n21 \u003c/bookstore\u003e\u003c/p\u003e\n\u003cp\u003e\u003cimg class='lazyload' data-src=\"https://article.cdnof.com/2307/0b447e39-ac4a-4437-b8d0-415c7b87fe24.gif\" alt=\"\" /\u003e\u003c/p\u003e\n\u003cp\u003e  再如上例xml文档:\u003c/p\u003e\n\u003cp\u003e\u003cimg class='lazyload' data-src=\"https://article.cdnof.com/2307/d6a1e2bf-54a0-4c32-a321-7e26aecd5d42.jpg\" alt=\"\" /\u003e\u003c/p\u003e\n\u003cp\u003e  例子中的根元素是 \u003cbookstore\u003e。文档中的所有 \u003cbook\u003e 元素都被包含在 \u003cbookstore\u003e 中。\u003cbook\u003e 元素有 4 个子元素:\u003ctitle\u003e、\u003c author\u003e、\u003cyear\u003e、\u003cprice\u003e。\u003c/p\u003e\n\u003ch3 id=\"(2)xml的特点\"\u003e(2)xml的特点\u003c/h3\u003e\n\u003cp\u003e  a).\u0026nbsp;XML 的属性值须加引号。\u003c/p\u003e\n\u003cp\u003e  ·\u0026nbsp;与 HTML 类似,XML 也可拥有属性(名称/值的对)。\u003c/p\u003e\n\u003cp\u003e  ·\u0026nbsp;在 XML 中,XML 的属性值须加引号。\u003c/p\u003e\n\u003cp\u003e  b).\u0026nbsp;XML 文档必须有根元素.XML 文档必须有一个元素是所有其他元素的\u003cstrong\u003e父元素\u003c/strong\u003e。该元素称为\u003cstrong\u003e根元素\u003c/strong\u003e。\u003c/p\u003e\n\u003cp\u003e  c).\u0026nbsp;XML 标签对大小写敏感.\u003c/p\u003e\n\u003cp\u003e  ·\u0026nbsp;XML 元素使用 XML 标签进行定义。\u003c/p\u003e\n\u003cp\u003e  ·\u0026nbsp;XML 标签对大小写敏感。在 XML 中,标签 \u003cLetter\u003e 与标签 \u003cletter\u003e 是不同的。必须使用相同的大小写来编写打开标签和关闭标签。\u003c/p\u003e\n\u003cp\u003e  d).\u0026nbsp;XML 中的注释语法: \u003c/p\u003e\n\u003c!-- This is a comment --\u003e\n\u003cp\u003e  e).\u0026nbsp;XML 元素 vs. 属性\u003c/p\u003e\n\u003cp\u003e\u003cimg class='lazyload' data-src=\"https://article.cdnof.com/2307/4b79a7ab-6d9b-4512-891b-0f7ac92041e3.gif\" alt=\"\" /\u003e\u003c/p\u003e\n\u003cp\u003e1 \u003cperson sex=\"female\"\u003e \n 2 \u003cfirstname\u003eAnna\u003c/firstname\u003e \n 3 \u003clastname\u003eSmith\u003c/lastname\u003e \n 4 \u003c/person\u003e \u003cbr /\u003e\n 5 \u003cbr /\u003e\n 6 \u003cperson\u003e \n 7 \u003csex\u003efemale\u003c/sex\u003e \n 8 \u003cfirstname\u003eAnna\u003c/firstname\u003e \n 9 \u003clastname\u003eSmith\u003c/lastname\u003e \n10 \u003c/person\u003e \u003c/p\u003e\n\u003cp\u003e\u003cimg class='lazyload' data-src=\"https://article.cdnof.com/2307/a9f71d11-a6d1-4860-a6e7-e8bfc886bf99.gif\" alt=\"\" /\u003e\u003c/p\u003e\n\u003cp\u003e  在第一行中,sex 是一个属性。在第7行中,sex 则是一个子元素。两个例子均可提供相同的信息。\u003c/p\u003e\n\u003ch1 id=\"2-xml-解析\"\u003e2. xml 解析\u003c/h1\u003e\n\u003cp\u003e  xml解析方法有四种:  \u003c/p\u003e\n\u003cp\u003e  ·\u0026nbsp;DOM(JAXP Crimson 解析器):W3C为HTML和XML分析器制定的标准接口规范,基于树,可随机动态访问和更新文档的内容、结构、样式。\u003c/p\u003e\n\u003cp\u003e  ·\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"http://sax.sourceforge.net/\"\u003eSAX\u003c/a\u003e(simple API for XML):不是W3C的标准,而是由XML-DEV邮件列表成员于1998年为Java语言开发的一种基于事件的简单API。基于事件,逐行解析,顺序访问XML文档,速度快,处理功能简单。\u003c/p\u003e\n\u003cp\u003e  \u0026nbsp;·\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"http://www.jdom.org/\"\u003eJDOM\u003c/a\u003e:鉴于DOM的低效率,而SAX又不能随机处理XML文档,Jason Hunter与Brett McLaughlin于2000年春天,开始创建一种能充分体现两者优势的\u003ca rel=\"nofollow noopener noreferrer\" href=\"http://sourceforge.net/projects/dom4j\"\u003eAPI\u003c/a\u003e——\u003ca rel=\"nofollow noopener noreferrer\" href=\"http://www.jdom.org/\"\u003eJDOM\u003c/a\u003e(Java-based DOM,基于Java的DOM),它是一个基于Java的对象模型,树状结构,能使读取、操作和写入XML文档,比DOM更高效,比SAX更强大,但由于使用了大量的类而不使用接口导致灵活性降低。\u003c/p\u003e\n\u003cp\u003e  \u003ca rel=\"nofollow noopener noreferrer\" href=\"http://sourceforge.net/projects/dom4j\"\u003e·\u0026nbsp;DOM4J\u003c/a\u003e:DOM4J是一个易用的,开源的库,用于XML,XPath,XSLT。它应用于Java平台,采用了Java集合框架并完全支持DOM,SAX,JAXP。它提供了大量的接口,因此比JDOM更具有灵活性。\u003c/p\u003e\n\u003cp\u003e  DOM(Document Object Model文档对象模型),是W3C为HTML和XML分析器制定的标准接口规范。 \u003c/p\u003e\n\u003cp\u003e\u003cstrong\u003e特点:\u003c/strong\u003e独立于语言,跨平台(可以在各种编程和脚本语言中使用),需要将整个文档读入内存,在内存中创建文档树,可随即访问文档中的特定节点,对内存的要求比较高,经过测试,访问速度相对于其他解析方式较慢,适用于简单文档的随即处理。\u003c/p\u003e\n\u003cp\u003e常用的节点属性:\u003c/p\u003e\n\u003cp\u003e\u003cstrong\u003e属性\u003c/strong\u003e\u003c/p\u003e\n\u003cp\u003e\u003cstrong\u003e描述\u003c/strong\u003e\u003c/p\u003e\n\u003cp\u003enodeName\u003c/p\u003e\n\u003cp\u003e结点名称\u003c/p\u003e\n\u003cp\u003enodeValue\u003c/p\u003e\n\u003cp\u003e结点内部值,通常只应用于文本结点\u003c/p\u003e\n\u003cp\u003enodeType\u003c/p\u003e\n\u003cp\u003e节点类型对应的数字\u003c/p\u003e\n\u003cp\u003eparentNode\u003c/p\u003e\n\u003cp\u003e如果存在,指向当前结点的父亲结点\u003c/p\u003e\n\u003cp\u003echildNodes\u003c/p\u003e\n\u003cp\u003e子结点列表\u003c/p\u003e\n\u003cp\u003efirstChild\u003c/p\u003e\n\u003cp\u003e如果存在,指向当前元素的第一个子结点\u003c/p\u003e\n\u003cp\u003elastChild\u003c/p\u003e\n\u003cp\u003e如果存在,指向当前元素的最后一个子结点\u003c/p\u003e\n\u003cp\u003epreviousSibling\u003c/p\u003e\n\u003cp\u003e指向当前结点的前一个兄弟结点\u003c/p\u003e\n\u003cp\u003enextSibling\u003c/p\u003e\n\u003cp\u003e指向当前结点的后一个兄弟结点\u003c/p\u003e\n\u003cp\u003eattributes\u003c/p\u003e\n\u003cp\u003e元素的属性列表\u003c/p\u003e\n\u003cp\u003e常用的节点方法:\u003c/p\u003e\n\u003cp\u003e\u003cstrong\u003e操作类型\u003c/strong\u003e\u003c/p\u003e\n\u003cp\u003e\u003cstrong\u003e方法原型\u003c/strong\u003e\u003c/p\u003e\n\u003cp\u003e\u003cstrong\u003e描述\u003c/strong\u003e\u003c/p\u003e\n\u003cp\u003e访问节点\u003c/p\u003e\n\u003cp\u003egetElementById(id)\u003c/p\u003e\n\u003cp\u003e根据ID属性查找元素节点\u003c/p\u003e\n\u003cp\u003egetElementsByName(name)\u003c/p\u003e\n\u003cp\u003e根据name属性查找元素集\u003c/p\u003e\n\u003cp\u003egetElementsByTagName(tagName)\u003c/p\u003e\n\u003cp\u003e根据元素标记名称查找元素集\u003c/p\u003e\n\u003cp\u003e创建节点\u003c/p\u003e\n\u003cp\u003ecreateElement(tagName)\u003c/p\u003e\n\u003cp\u003e创建元素节点\u003c/p\u003e\n\u003cp\u003ecreateTestNode(string)\u003c/p\u003e\n\u003cp\u003e创建文本节点\u003c/p\u003e\n\u003cp\u003ecreateAttribute(name)\u003c/p\u003e\n\u003cp\u003e创建属性节点\u003c/p\u003e\n\u003cp\u003e插入和添加节点\u003c/p\u003e\n\u003cp\u003eappendChild(newChild)\u003c/p\u003e\n\u003cp\u003e添加子节点到目标节点上\u003c/p\u003e\n\u003cp\u003einsertBefore(newChild,targetChild)\u003c/p\u003e\n\u003cp\u003e将newChild节点插入到targetChild节点之前\u003c/p\u003e\n\u003cp\u003e复制节点\u003c/p\u003e\n\u003cp\u003eCloneNode(bool)\u003c/p\u003e\n\u003cp\u003e复制该节点,由bool确定是否复制子节点\u003c/p\u003e\n\u003cp\u003e删除和替换节点\u003c/p\u003e\n\u003cp\u003eremoveChild(childName)\u003c/p\u003e\n\u003cp\u003e删除由childName指定的节点\u003c/p\u003e\n\u003cp\u003ereplaceChild(newChild,oldChild)\u003c/p\u003e\n\u003cp\u003e用newChild替换oldChild\u003c/p\u003e\n\u003cp\u003e属性节点操作\u003c/p\u003e\n\u003cp\u003egetAttribute(name)\u003c/p\u003e\n\u003cp\u003e返回目标对象指定属性名称为name的属性值\u003c/p\u003e\n\u003cp\u003esetAttribute(name,value)\u003c/p\u003e\n\u003cp\u003e修改目标节点指定属性名称为name的属性值为value\u003c/p\u003e\n\u003cp\u003eremoveAttribute(name)\u003c/p\u003e\n\u003cp\u003e删除目标节点指定属性名称为name的属性\u003c/p\u003e\n\u003cp\u003e(1)读取本地xml文档解析为对象的步骤: \u003c/p\u003e\n\u003cp\u003e  * 首先利用DocumentBuilderFactory创建一个DocumentBuilderFactory实例;然后利用DocumentBuilderFactory创建DocumentBuilder\u003c/p\u003e\n\u003cp\u003e//创建DocumentBuilderFactory工厂实例。 \u003cbr /\u003e\nDocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance(); \u003cbr /\u003e\n//通过文档创建工厂创建文档创建器 \u003cbr /\u003e\nDocumentBuilder dBuilder = dbfactory.newDocumentBuilder();\u003c/p\u003e\n\u003cp\u003e  * 然后加载XML文档(Document) \u003cstrong\u003e:\u003c/strong\u003e 通过文档创建器DocumentBuilder的parse方法解析参数URL指定的XML文档,并返回一个Document 对象。\u003c/p\u003e\n\u003cp\u003eDocument doc = dBuilder.parse(url);\u003c/p\u003e\n\u003cp\u003e  * 然后获取文档的根结点(Element), \u003cbr /\u003e\n  * 然后获取根结点中所有子节点的列表(NodeList), \u003cbr /\u003e\n  * 然后使用再获取子节点列表中的需要读取的结点。\u003c/p\u003e\n\u003cp\u003e\u003cstrong\u003e在实际开发中多思考,灵活运用,非是难事儿!,对于程序员来讲,精彩的代码是如何想出来的,远比看到精彩的代码更加令人期望!\u003c/strong\u003e\u003c/p\u003e\n\u003cp\u003e\u003c?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?\u003e \u003cbr /\u003e\n\u003cbooks\u003e \n \u003cbook typeId=\"1\"\u003e \n \u003cbookId\u003e001\u003c/bookId\u003e \n \u003cbookName\u003eMaven实战\u003c/bookName\u003e \n \u003cbookPrice\u003e51\u003c/bookPrice\u003e \n \u003cbookAuthor\u003e陈晓斌\u003c/bookAuthor\u003e \n \u003c/book\u003e \n \u003cbook typeId=\"2\"\u003e \n \u003cbookId\u003e002\u003c/bookId\u003e \n \u003cbookName\u003e谈谈支付宝底层体系架构的大家\u003c/bookName\u003e \n \u003cbookPrice\u003e151\u003c/bookPrice\u003e \n \u003cbookAuthor\u003e程立\u003c/bookAuthor\u003e \n \u003c/book\u003e \n \u003cbook TypeId=\"3\"\u003e \n \u003cbookId\u003e003\u003c/bookId\u003e \n \u003cbookName\u003e预约死亡\u003c/bookName\u003e \n \u003cbookPrice\u003e99\u003c/bookPrice\u003e \n \u003cbookAuthor\u003e毕淑敏\u003c/bookAuthor\u003e \n \u003c/book\u003e \n \u003cbook TypeId=\"3\"\u003e \n \u003cbookId\u003e003\u003c/bookId\u003e \n \u003cbookName\u003e预约死亡\u003c/bookName\u003e \n \u003cbookPrice\u003e56\u003c/bookPrice\u003e \n \u003cbookAuthor\u003e毕淑敏\u003c/bookAuthor\u003e \n \u003c/book\u003e \n\u003c/books\u003e\u003c/p\u003e\n\u003cp\u003eimport javax.xml.parsers.DocumentBuilder; \u003cbr /\u003e\nimport javax.xml.parsers.DocumentBuilderFactory; \u003cbr /\u003e\nimport javax.xml.parsers.ParserConfigurationException; \u003cbr /\u003e\nimport javax.xml.transform.Result; \u003cbr /\u003e\nimport javax.xml.transform.Source; \u003cbr /\u003e\nimport javax.xml.transform.Transformer; \u003cbr /\u003e\nimport javax.xml.transform.TransformerFactory; \u003cbr /\u003e\nimport javax.xml.transform.dom.DOMSource; \u003cbr /\u003e\nimport javax.xml.transform.stream.StreamResult;\u003c/p\u003e\n\u003cp\u003eimport org.w3c.dom.Document; \u003cbr /\u003e\nimport org.w3c.dom.Element; \u003cbr /\u003e\nimport org.w3c.dom.Node; \u003cbr /\u003e\nimport org.w3c.dom.NodeList;\u003c/p\u003e\n\u003cp\u003epublic class Test { \u003cbr /\u003e\n /** \u003cbr /\u003e\n * XML读取 \u003cbr /\u003e\n * \u003ca rel=\"nofollow noopener noreferrer\" href=\"https://github.com/throws\"\u003e@throws\u003c/a\u003e Exception \u003cbr /\u003e\n */ \u003cbr /\u003e\n public static void xml() throws Exception{ \u003cbr /\u003e\n //1.构建一个工厂 \u003cbr /\u003e\n DocumentBuilderFactory dbFactory =DocumentBuilderFactory.newInstance(); \u003cbr /\u003e\n //2.构建builder1 \u003cbr /\u003e\n DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder(); \u003cbr /\u003e\n //3.Document对象 \u003cbr /\u003e\n //一般放相对路径就可以了,程序自动在项目下寻址 \u003cbr /\u003e\n Document document = dbBuilder.parse(\"books.xml\"); \u003cbr /\u003e\n //4.XML解析 \u003cbr /\u003e\n NodeList list =document.getElementsByTagName(\"book\"); \u003cbr /\u003e\n for (int i = 0; i \u0026lt; list.getLength(); i++) { \u003cbr /\u003e\n Element item = (Element)list.item(i); \u003cbr /\u003e\n String attribute=item.getAttribute(\"typeId\"); \u003cbr /\u003e\n String bookId=item.getElementsByTagName(\"bookId\").item(0).getTextContent(); \u003cbr /\u003e\n String bookName=item.getElementsByTagName(\"bookName\").item(0).getTextContent(); \u003cbr /\u003e\n String bookPrice=item.getElementsByTagName(\"bookPrice\").item(0).getTextContent(); \u003cbr /\u003e\n String bookAuthor=item.getElementsByTagName(\"bookAuthor\").item(0).getTextContent(); \u003cbr /\u003e\n System.out.println(bookId); \u003cbr /\u003e\n System.out.println(bookName); \u003cbr /\u003e\n System.out.println(bookPrice); \u003cbr /\u003e\n System.out.println(bookAuthor); \u003cbr /\u003e\n //System.out.println(attribute); \u003cbr /\u003e\n System.out.println(\"-----------------\"); \u003cbr /\u003e\n } \u003cbr /\u003e\n } \u003cbr /\u003e\n /** \u003cbr /\u003e\n * XML添加 \u003cbr /\u003e\n * \u003ca rel=\"nofollow noopener noreferrer\" href=\"https://github.com/throws\"\u003e@throws\u003c/a\u003e Exception \u003cbr /\u003e\n */ \u003cbr /\u003e\n public static void insert() throws Exception{ \u003cbr /\u003e\n //1.构建一个工厂 \u003cbr /\u003e\n DocumentBuilderFactory dbFactory =DocumentBuilderFactory.newInstance(); \u003cbr /\u003e\n //2.构建builder1 \u003cbr /\u003e\n DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder(); \u003cbr /\u003e\n //3.Document对象 \u003cbr /\u003e\n Document document = dbBuilder.parse(\"books.xml\"); \u003cbr /\u003e\n //1.伪造内存中的节点,游离节点 \u003cbr /\u003e\n Element book = document.createElement(\"book\"); \u003cbr /\u003e\n book.setAttribute(\"TypeId\", \"3\"); \u003cbr /\u003e\n Element bookId=document.createElement(\"bookId\"); \u003cbr /\u003e\n bookId.setTextContent(\"003\"); \u003cbr /\u003e\n Element bookName = document.createElement(\"bookName\"); \u003cbr /\u003e\n bookName.setTextContent(\"预约死亡\"); \u003cbr /\u003e\n Element bookPrice = document.createElement(\"bookPrice\"); \u003cbr /\u003e\n bookPrice.setTextContent(\"56\"); \u003cbr /\u003e\n Element bookAuthor = document.createElement(\"bookAuthor\"); \u003cbr /\u003e\n bookAuthor.setTextContent(\"毕淑敏\"); \u003cbr /\u003e\n book.appendChild(bookId); \u003cbr /\u003e\n book.appendChild(bookName); \u003cbr /\u003e\n book.appendChild(bookPrice); \u003cbr /\u003e\n book.appendChild(bookAuthor);\u003c/p\u003e\n\u003cpre\u003e\u003ccode\u003e //1.将Book节点和整篇文档建立关联 \n document.getElementsByTagName(\"books\").item(0).appendChild(book); \n //1.传输工厂 \n TransformerFactory transformerFactory = TransformerFactory.newInstance(); \n //2.传输器 \n Transformer transformer = transformerFactory.newTransformer(); \n Source source = new DOMSource(document); \n StreamResult result = new StreamResult(\"books.xml\"); \n //3.传输方法 \n transformer.transform(source, result); \n System.out.println(\"save ok!\"); \n} \n/\\*\\* \n \\* XML修改 \n \\* @throws Exception \n \\*/ \npublic static void update() throws Exception{ \n //1.构建一个工厂 \n DocumentBuilderFactory dbFactory =DocumentBuilderFactory.newInstance(); \n //2.构建builder1 \n DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder(); \n //3.Document对象 \n Document document = dbBuilder.parse(\"books.xml\"); \n //修改指定item的书的价格 \n //\"E:/eclispsespace/JavaOOP-Day-09-0100/books.xml\" \n Element book = (Element)document.getElementsByTagName(\"book\").item(2); \n book.getElementsByTagName(\"bookPrice\").item(0).setTextContent(\"99\"); \n //传输工程 \n TransformerFactory transformerFactory = TransformerFactory.newInstance(); \n //传输器 \n Transformer transformer =transformerFactory.newTransformer(); \n Source source = new DOMSource(document); \n Result result = new StreamResult(\"books.xml\"); \n transformer.transform(source, result); \n System.out.println(\"修改完毕!\"); \n} \n/\\*\\* \n \\* XML删除 \n \\* @throws Exception \n \\*/ \npublic static void delXml() throws Exception{ \n //1.构建一个工厂 \n DocumentBuilderFactory dbFactory =DocumentBuilderFactory.newInstance(); \n //2.构建builder1 \n DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder(); \n //3.Document对象 \n //一般放相对路径就可以了,程序自动在项目下寻址 \n Document document = dbBuilder.parse(\"books.xml\"); \n Element book = (Element)document.getElementsByTagName(\"book\").item(2); \n document.getElementsByTagName(\"books\").item(0).removeChild(book); \n //传输工程 \n TransformerFactory transformerFactory = TransformerFactory.newInstance(); \n //传输器 \n Transformer transformer = transformerFactory.newTransformer(); \n Source source = new DOMSource(document); \n Result result =new StreamResult(\"books.xml\"); \n transformer.transform(source, result); \n System.out.println(\"删除成功!\");\n\n} \npublic static void main(String\\[\\] args) throws Exception { \n //xml();//读取 \n //insert();//增加 \n //update();//修改 \n delXml() ;//删除\n\n}\u003c/code\u003e\u003c/pre\u003e\n\u003cp\u003e}\u003c/p\u003e\n\u003ch3 id=\"dom4j:\"\u003eDom4j:\u003c/h3\u003e\n\u003cp\u003eDom4j是一个Java的XML API,是jdom的升级品,用来读写XML文件的。dom4j是一个十分优秀的JavaXML API,具有性能优异、功能强大和极其易使用的特点,它的性能超过sun公司官方的dom技术,同时它也是一个开放源代码的软件,可以在SourceForge上找到它。在IBM developerWorks上面还可以找到一篇文章,对主流的Java XML API进行的性能、功能和易用性的评测,所以可以知道dom4j无论在哪个方面都是非常出色的。如今可以看到越来越多的Java软件都在使用dom4j来读写XML,特别值得一提的是连Sun的JAXM也在用dom4j。这已经是必须使用的jar包,Hibernate也用它来读写文件。\u003c/p\u003e\n\u003cp\u003e所需jar包:dom4j-1.6.1.jar\u003c/p\u003e\n\u003ch3 id=\"实例图:\"\u003e实例图:\u003c/h3\u003e\n\u003cp\u003e\u003cimg class='lazyload' data-src=\"https://article.cdnof.com/2307/e2a69ebc-a945-47e8-9525-daa895383c29.png\" alt=\"\" /\u003e\u003c/p\u003e\n\u003ch3 id=\"xml文件:\"\u003eXML文件:\u003c/h3\u003e\n\u003cp\u003e\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e \u003cbr /\u003e\n\u003cpeople city=\"beijing\"\u003e \n \u003cstudent name=\"milton\" age=\"22\"\u003e\u003c/student\u003e \n \u003cstudent name=\"lego\" age=\"23\"\u003e\u003c/student\u003e \n \u003cteacher name=\"bruce\" age=\"27\"\u003e\u003c/teacher\u003e \n \u003cteacher name=\"ziven\" age=\"29\"\u003e\u003c/teacher\u003e \n\u003c/people\u003e\u003c/p\u003e\n\u003ch3 id=\"对应entity:\"\u003e对应entity:\u003c/h3\u003e\n\u003ch3 id=\"people类:\"\u003ePeople类:\u003c/h3\u003e\n\u003cp\u003e\u003cimg class='lazyload' data-src=\"https://article.cdnof.com/2307/270e2f57-2acd-4483-b3fd-d87ae4604fb9.png\" alt=\"\" /\u003e\u003c/p\u003e\n\u003ch3 id=\"student类:\"\u003eStudent类:\u003c/h3\u003e\n\u003cp\u003e\u003cimg class='lazyload' data-src=\"https://article.cdnof.com/2307/5fc0d719-e7aa-4a52-9db8-d5158cfb294a.png\" alt=\"\" /\u003e\u003c/p\u003e\n\u003ch3 id=\"teacher类:\"\u003eTeacher类:\u003c/h3\u003e\n\u003cp\u003e\u003cimg class='lazyload' data-src=\"https://article.cdnof.com/2307/e51d3dcd-78c6-4b40-b735-78d23c45802b.png\" alt=\"\" /\u003e\u003c/p\u003e\n\u003ch3 id=\"testxml类\"\u003eTestXml类:\u003c/h3\u003e\n\u003cp\u003epackage cn.resolve.dom4j; \u003cbr /\u003e\nimport java.io.File; \u003cbr /\u003e\nimport java.util.ArrayList; \u003cbr /\u003e\nimport java.util.Iterator; \u003cbr /\u003e\nimport java.util.List; \u003cbr /\u003e\nimport org.dom4j.Document; \u003cbr /\u003e\nimport org.dom4j.DocumentException; \u003cbr /\u003e\nimport org.dom4j.Element; \u003cbr /\u003e\nimport org.dom4j.io.SAXReader; \u003cbr /\u003e\nimport cn.resolve.entity.People; \u003cbr /\u003e\nimport cn.resolve.entity.Student; \u003cbr /\u003e\nimport cn.resolve.entity.Teacher; \u003cbr /\u003e\n/** \u003cbr /\u003e\n * 测试使用dom4j解析XML \u003cbr /\u003e\n * \u003ca rel=\"nofollow noopener noreferrer\" href=\"https://github.com/author\"\u003e@author\u003c/a\u003e Zhang \u003cbr /\u003e\n * \u003cbr /\u003e\n */ \u003cbr /\u003e\npublic class TestXml { \u003cbr /\u003e\n public static People xmlParse(String xmlPath){ \u003cbr /\u003e\n File xmlFile = new File(xmlPath); \u003cbr /\u003e\n System.out.println(xmlFile.getPath()); \u003cbr /\u003e\n //判断文件是否存在 \u003cbr /\u003e\n if (xmlFile.exists()) { \u003cbr /\u003e\n SAXReader reader = new SAXReader(); \u003cbr /\u003e\n People people = new People(); \u003cbr /\u003e\n try { \u003cbr /\u003e\n //读入文档流 \u003cbr /\u003e\n Document document = reader.read(xmlFile); \u003cbr /\u003e\n //获取根节点 \u003cbr /\u003e\n Element root =document.getRootElement(); \u003cbr /\u003e\n //创建两个对象集合 \u003cbr /\u003e\n List\u003cStudent\u003e students = new ArrayList\u003cStudent\u003e(); \u003cbr /\u003e\n List\u003cTeacher\u003e teachers = new ArrayList\u003cTeacher\u003e(); \u003cbr /\u003e\n //拿到attribute属性的值 \u003cbr /\u003e\n people.setCity(root.attributeValue(\"city\")); \u003cbr /\u003e\n //解析student节点 \u003cbr /\u003e\n for (Iterator iterator = root.elementIterator(\"student\");iterator.hasNext();) { \u003cbr /\u003e\n Element eStudent= (Element)iterator.next(); \u003cbr /\u003e\n Student student = new Student(eStudent.attributeValue(\"name\"),Integer.parseInt(eStudent.attributeValue(\"age\"))); \u003cbr /\u003e\n //添加到集合 \u003cbr /\u003e\n students.add(student); \u003cbr /\u003e\n } \u003cbr /\u003e\n //解析teacher节点 \u003cbr /\u003e\n for (Iterator iterator= root.elementIterator(\"teacher\");iterator.hasNext();) { \u003cbr /\u003e\n Element eTeacher = (Element)iterator.next(); \u003cbr /\u003e\n Teacher teacher = new Teacher(eTeacher.attributeValue(\"name\"),Integer.parseInt(eTeacher.attributeValue(\"age\"))); \u003cbr /\u003e\n teachers.add(teacher); \u003cbr /\u003e\n } \u003cbr /\u003e\n people.setStudent(students); \u003cbr /\u003e\n people.setTeacher(teachers); \u003cbr /\u003e\n System.out.println(\"success!\"); \u003cbr /\u003e\n } catch (DocumentException e) { \u003cbr /\u003e\n e.printStackTrace(); \u003cbr /\u003e\n } \u003cbr /\u003e\n return people; \u003cbr /\u003e\n }else { \u003cbr /\u003e\n System.out.println(\"file is not exist!\"); \u003cbr /\u003e\n return null; \u003cbr /\u003e\n } \u003cbr /\u003e\n } \u003cbr /\u003e\n public static void main(String[] args) { \u003cbr /\u003e\n String xmlpath = \"src/people.xml\"; \u003cbr /\u003e\n //解析people.xml \u003cbr /\u003e\n People people =xmlParse(xmlpath); \u003cbr /\u003e\n System.out.println(\"file full path is xmlpath \"); \u003cbr /\u003e\n List\u003cTeacher\u003e teachers = new ArrayList\u003cTeacher\u003e(); \u003cbr /\u003e\n List\u003cStudent\u003e students = new ArrayList\u003cStudent\u003e(); \u003cbr /\u003e\n System.out.println(people.getCity()); \u003cbr /\u003e\n //处理解析结果 \u003cbr /\u003e\n for (Student s : students) { \u003cbr /\u003e\n System.out.println(\"学生姓名/t/t年龄\"); \u003cbr /\u003e\n System.out.println(s.getName()+\":\"+s.getAge()); \u003cbr /\u003e\n } \u003cbr /\u003e\n for (Teacher t: teachers) { \u003cbr /\u003e\n System.out.println(\"教师姓名/t/t年龄\"); \u003cbr /\u003e\n System.out.println(); \u003cbr /\u003e\n } \u003cbr /\u003e\n } \u003cbr /\u003e\n}\u003c/p\u003e"},"seo":{"title":"JavaXML解析的四种方法(连载)","description":"1. xml简介\n  XML:指可扩展标记语言, Extensible Markup Language;类似HTML。XML的设计宗旨是传输数据,而非显示数据。\n  一个xml文档实例:\n\n\n 1 \u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\n 2 \u003ccompany name=\"Tencent\" address=\"深圳市南山区\"\u003e\n 3 \u003cdepartment ","image":"https://article.cdnof.com/2307/1630751f-933e-422b-a1de-f206ac7afa46.gif","url":"https://v2as.com/article/4b01e1ed-2481-41f1-8738-60b50a17caed","keywords":["XML","xml","节点","文档","book","解析","import","元素","document","Element"]},"viewsCount":1,"promotionList":[{"title":"阿里云服务器","image":"https://article.cdnof.com/promotion/aliyun.jpg","link":"https://qd.rs/aliyun"},{"title":"腾讯云服务器","image":"https://article.cdnof.com/promotion/tencent.jpg","link":"https://qd.rs/tencent"},{"title":"七牛云服务器","image":"https://article.cdnof.com/promotion/qiniu.png","link":"https://qd.rs/qiniu"}],"similarKeywordsList":[{"article_id":"1f20385e-eaa9-4f43-b079-64c430020b86","title":"ASP.NET 网站管理工具","link":"","description":"ylbtech-Miscellaneos:ASP.NET 网站管理工具\n\n\n\n\n\n\n1. 网站管理工具概述返回顶部 \n\n\n\n\n网站管理工具概述\n\n使用网站管理工具,可以通过一个简单的 Web 界面来查看和管理网站配置。\n\n下面的链接提供了有关如何使用网站管理工具的更多信息:\n网站管理工具“安全”选项卡\n网站管理工具“应用程序”选项卡\n网站管理工具“提供程序”选项卡\n网站管理工具的内部机制\n\n网站配","image":"https://article.cdnof.com/2009/88495ffb-89dd-4d30-9848-a7a05491f874.jpg","keywords":["管理工具","网站","应用程序","NET","Web","设置","ASP","选项卡","用户","使用"],"created_at":"0001-01-01T00:00:00Z"},{"article_id":"71ae3035-d6b6-4775-aaab-7e12f232edb9","title":"Redis进阶实践之十九 Redis如何使用lua脚本","link":"","description":"一、引言\nredis学了一段时间了,基本的东西都没问题了。从今天开始讲写一些redis和lua脚本的相关的东西,lua这个脚本是一个好东西,可以运行在任何平台上,也可以嵌入到大多数语言当中,来扩展其功能。lua脚本是用C语言写的,体积很小,运行速度很快,并且每次的执行都是作为一个原子事务来执行的,我们可以在其中做很多的事情。由于篇幅很多,一次无法概述全部,这个系列可能要通过多篇文章的形式来写,好了","image":"","keywords":["Redis","脚本","Lua","redis","命令","script","执行","EVAL","调用","return"],"created_at":"0001-01-01T00:00:00Z"},{"article_id":"5240ba5c-dcee-481a-b9b2-b83bd88a5710","title":"log4j2同步日志引发的性能问题","link":"","description":"1 问题回顾\n1.1 问题描述\n在项目的性能测试中,相关的接口的随着并发数增加,接口的响应时间变长,接口吞吐不再增长,应用的CPU使用率较高。\n1.2 分析思路\n谁导致的CPU较高,阻塞接口TPS的增长?接口的响应时间的调用链分布是什么样的,有没有慢的点?\n1)使用火焰图分析应用的CPU如下,其中log4j2日志占了40%左右CPU,初步怀疑是log4j2的问题。\n\n2)调用链的分析\n通过pfin","image":"https://article.cdnof.com/2310/9b2531c1-b0e3-4988-8a4b-6d04333dad00.jpg","keywords":["日志","异步","log4j2","输出","AsyncLogger","线程","同步","AsyncAppender","使用","性能"],"created_at":"0001-01-01T00:00:00Z"},{"article_id":"aacfe9d2-c6a0-4081-8032-418604f6a6b5","title":"hadoop集群搭建及编程实践","link":"","description":"Hadoop集群搭建\n前期准备及JDK,hadoop安装\n设置主机名和添加主机映射\n验证连通性\nSSH无密码登录\n配置集群/分布式环境\n\n\n\n\n\n分发到其他结点\n格式化namenode\n执行分布式实例\njava API与HDFS的编程\n\n\n\n\n\n1.1JDK的下载地址,hadoop下载地址\nJava Downloads | Oracle 中国\n选择JDK8\n清华镜像源\n选择hadoop-3.3.5","image":"https://article.cdnof.com/2310/7316603c-cf4a-4fc2-b619-88e1702fa286.png","keywords":["hadoop","fs","new","String","prettyspider","集群","HDFS","import","public","sudo"],"created_at":"0001-01-01T00:00:00Z"},{"article_id":"7c4258c6-9fe1-4560-b3b7-5eacb6a5872d","title":"一键安装lnmp 环境","link":"","description":"一键安装lnmp 环境\n\n目录\n一键安装lnmp 环境\n操作步骤\n1.添加网站(虚拟主机)\n2.伪静态管理\n3.上传网站程序\n4.已存在虚拟主机添加ssl证书开启https\n5.列出网站(虚拟主机)\n6.删除网站(虚拟主机)\n7.默认网站(虚拟主机)\n8.防跨目录设置\n9.pathinfo设置\n10.数据库管理\n\nyum.repos.d镜像\n[root@cloud back2]# cat Cent","image":"https://article.cdnof.com/2309/9d6ca58a-34a9-4036-880b-8d8bd3391221.jpg","keywords":["php","php74","pecl","PHP","conf","x86","64","lnmp","usr","local"],"created_at":"0001-01-01T00:00:00Z"},{"article_id":"ba2ff4a0-6b69-421b-b077-3a554853f5a6","title":"提高 Web 开发效率的10个VS Code扩展插件,你知道吗?","link":"","description":"前言\n一个出色的开发工具可以显著提高开发人员的开发效率,而优秀的扩展插件则能更进一步地提升工具的效率。在前端开发领域,VSCode毫无疑问是目前最受欢迎的开发工具。为了帮助前端开发人员提高工作效率,今天小编将向大家推荐10个强大的VSCode扩展插件。\n1.JavaScript (ES6) Code Snippets\nJavaScript(ES6)代码片段插件可以帮助开发人员节省时间:开发人员可以","image":"https://article.cdnof.com/2309/d0e74479-9d9d-4770-91fe-488bc3000e5b.png","keywords":["插件","console","开发","控制台","效率","Code","VS","代码","Web","object"],"created_at":"0001-01-01T00:00:00Z"},{"article_id":"09a74eb1-ca7a-43ae-b13e-92020f285c7b","title":"openNebula集群搭建","link":"","description":"openNebula集群搭建\n\n目录\nopenNebula集群搭建\nOpenNebula概述\n环境介绍及部署前准备\n1. 安装步骤\n1.关闭防火墙\n2.配置epel源地和opennebula源\n3.安装软件\n4、Ruby 运行环境安装(V5.10版本之后可选)\n5、mysql数据库配置\n6、启动OpenNebula\n2、Opennedula node节点安装\n1、要添加OpenNebula存储库,","image":"https://article.cdnof.com/2309/1d71da44-477c-44d4-abbb-9f9b854720af.png","keywords":["opennebula","OpenNebula","var","root","repo","lib","gems","集群","搭建","yum"],"created_at":"0001-01-01T00:00:00Z"},{"article_id":"70990184-a0be-4d44-b745-fed63d7f36cf","title":"cephadm快速部署指定版本ceph集群及生产问题处理","link":"","description":"cephadm快速部署指定版本ceph集群及生产问题处理\n\n目录\ncephadm快速部署指定版本ceph集群及生产问题处理\n1、虚拟机规划:centos8\n2、ceph版本:(安装指定版本在源里面指定即可)\n3、虚拟机操作系统:\n4、初始化工作(三台机器同时操作):\n4.1关闭防火墙:\n4.2 关闭SELinux:\n4.3 设置时间同步:\n4.4 配置epel源:\n4.5 设置主机名:\n4.6修改","image":"https://article.cdnof.com/2309/b0f72d2e-2223-401d-a391-e47aa93e8758.jpg","keywords":["ceph","rgw","cn","pool","beijing","root","ceph1","osd","default","com"],"created_at":"0001-01-01T00:00:00Z"},{"article_id":"187554b2-7be5-4da3-923a-283107e20136","title":"C# - XMLHelper :一个操作XML的简单类库","link":"","description":"下午写了一个操作XML文件的类库,后来不用了,水篇文章存个档\n\nXMLHelper.cs主要提供以下功能:\n加载XML文件:从文件路径或字符串中加载XML文档,并返回XmlDocument对象。\n保存XML文件:将XmlDocument对象保存为XML文件。\n读取XML文件到DataTable:将XML文件中的数据读取到DataTable对象中。\n生成XML文件:生成一个新的XML文件,并指定根节","image":"","keywords":["XML","XmlDocument","string","xmlDoc","节点","文件","DataTable","XPath","xpath","表达式"],"created_at":"0001-01-01T00:00:00Z"},{"article_id":"a965f5c3-f0a7-477a-b2d6-46ef08d3b3f3","title":"MQTT vs. XMPP,哪一个才是IoT通讯协议的正解","link":"","description":"这是个有趣的话题!\n先来聊几个小故事。\n关于我和MQTT\n我在人生第一个IoT项目里,第一次接触到MQTT协议。\n我很快就理解了这个协议。因为,它和企业开发用的MQ产品实在是太像了。\n在我职业生涯早期,是的,20年前,当时做一个银行的项目,就用过MQ这东西,那个项目使用了IBM MQ。随着工作经验的增加,在另外一些项目中,也会接触使用到MQ产品。\n后来,我在做国产中间件的公司里开发中间件,当时我们","image":"https://article.cdnof.com/2309/6b6fbffa-0ba4-45ee-b976-44ac813cb54f.png","keywords":["XMPP","MQTT","IoT","协议","class","loadModule","getName","通讯","QoS","开源"],"created_at":"0001-01-01T00:00:00Z"}]},"__N_SSG":true},"page":"/article/[article_id]","query":{"article_id":"4b01e1ed-2481-41f1-8738-60b50a17caed"},"buildId":"7EtL49Y65E8zx1NwcIC_o","isFallback":false,"gsp":true,"scriptLoader":[]}</script></body></html>