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);
        }
    }
}
时间: 2024-10-13 16:05:39

Java jaxp查询节点的相关文章

xml解析-jaxp查询结点

jaxp查询结点 eg://获取name的值 // person.xml <?xml version="1.0" encoding="UTF-8"?> <person> <p1> <name>zhangsan</name> <age>20</age> </p1> <p1> <name>lisi</name> <age>30&

java数据库查询结构

//获取数据表结构 public static void getConnAndTableStruct(){        Connection connection = null;        PreparedStatement pstmt = null;        ResultSetMetaData rsmd = null;        try {            //mysql连接             Class.forName("org.gjt.mm.mysql.Driv

java对xml节点属性的增删改查

学习本文之前请先看我的另一篇文章JAVA对XML节点的操作可以对XML操作有更好的了解. 1 package vastsum; 2 3 import java.io.File; 4 import java.io.FileWriter; 5 import java.util.Iterator; 6 7 import org.dom4j.Attribute; 8 import org.dom4j.Document; 9 import org.dom4j.Element; 10 import org.

java MongoDB查询(二)复杂查询

前言 在上篇<java MongoDB查询(一)简单查询>中我们简单了解了下查询,但是仅仅有那些查询是不够用的,还需要复杂的查询,这篇就这点进行叙述. 1.数据结构 集合:firstCollection 数据内容: { "_id" : ObjectId("55adba52fa1f3cf038c2aea6"), "name" : "user0", "age" : 22, "sex&quo

JAVA日期查询:季度、月份、星期等时间信息

参考:JAVA日期查询:季度.月份.星期等时间信息 import java.util.Calendar; public class Date { Calendar calendar = null; public void test(){ calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH) + 1; int season

Java 使用jaxp删除节点

<?xml version="1.0" encoding="UTF-8" standalone="no"?> <person> <p1> <name>jobs</name> <age>56</age> <sex>male</sex> </p1> <p1> <name>Alis</name> &

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> p

Java 使用jaxp修改节点

<?xml version="1.0" encoding="UTF-8" standalone="no"?> <person> <p1> <name>jobs</name> <age>56</age> <sex>female</sex> </p1> <p1> <name>Alis</name>

Hibernate异常之命名查询节点未找到

异常信息: 1 java.lang.IllegalArgumentException: No query defined for that name [salaryEmps] 2 at org.hibernate.internal.AbstractSharedSessionContract.getNamedQuery(AbstractSharedSessionContract.java:578) 3 at org.hibernate.internal.AbstractSharedSessionC