• Java jaxp查询节点


    <?xml version="1.0" encoding="UTF-8"?>
    <person>
        <p1>
            <name>jobs</name>
            <age>56</age>
        </p1>
        <p1>
            <name>Alis</name>
            <age>32</age>
        </p1>
    </person>
    package jaxp;
    
    import java.io.IOException;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;
    
    public class TestJaxp {
        public static void main(String[] args) {
            //创建解析器工厂
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
            //创建解析器
            DocumentBuilder builder = null;
            try {
                builder = builderFactory.newDocumentBuilder();
            } catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //解析xml返回document
            Document document = null;
            try {
                document = builder.parse("src/person.xml");
            } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // 得到name元素
            NodeList list = document.getElementsByTagName("name");
            for(int i = 0; i< list.getLength(); i++){
                Node node = list.item(i);
                //得到对应元素的值
                String str = node.getTextContent();
                System.out.println(str);
            }
        }
    }
  • 相关阅读:
    Web 前端 UI 组件库文档自动化方案 All In One
    how to auto open demo and create it in a new codesandbox
    TypeScript & Canvas 实现可视化白板
    2020~2021 职业规划书
    PostCSS All In One
    zsh terminal set infinity scroll height
    npm version ^ meaning
    vue-cli & webpack & vue.config.js
    [HDOJ5350]MZL's munhaff function
    [POJ3061]Subsequence
  • 原文地址:https://www.cnblogs.com/lantu1989/p/6163219.html
Copyright © 2020-2023  润新知