下面是编程之家 jb51.cc 通过网络收集整理的代码片段。
编程之家小编现在分享给大家,也给大家做个参考。
- org.w3c.dom W3C推荐的用于XML标准规划文档对象模型的接口。
- org.xml.sax用于对XML进行语法分析的事件驱动的XML简单API(SAX)
- javax.xml.parsers解析器工厂工具,程序员获得并配置特殊的特殊语法分析器。
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class DomParse { public DomParse(){ DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance(); try { DocumentBuilder dombuilder=domfac.newDocumentBuilder(); InputStream is=new FileInputStream("WebRoot/WEB-INF/hell.xml"); Document doc=dombuilder.parse(is); Element root=doc.getDocumentElement(); NodeList books=root.getChildNodes(); if(books!=null){ for(int i=0;i<books.getLength();i++){ Node book=books.item(i); if(book.getNodeType()==Node.ELEMENT_NODE){ String email=book.getAttributes().getNamedItem("email").getNodeValue(); System.out.println(email); for(Node node=book.getFirstChild();node!=null;node=node.getNextSibling()){ if(node.getNodeType()==Node.ELEMENT_NODE){ if(node.getNodeName().equals("name")){ String name=node.getNodeValue(); String name1=node.getFirstChild().getNodeValue(); System.out.println(name); System.out.println(name1); } if(node.getNodeName().equals("price")){ String price=node.getFirstChild().getNodeValue(); System.out.println(price); } } } } } } } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { new DomParse(); } }
以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。