Java 直接从XML获取孙子元素

Java 直接从XML获取孙子元素,java,xml,Java,Xml,我一直在寻找JAVA中的XML读取示例,我总是找到一个典型的模式根->3个等子->3个等子 但是有时候你只需要得到一对元素,它们不像一个图书馆那样存储着书{title,author,number of pages}。我的XML看起来像: <?xml version="1.0" encoding="UTF-8" ?> <A> <Crap1> <CrapSon>1</CrapSon> </Crap1&g

我一直在寻找JAVA中的XML读取示例,我总是找到一个典型的模式根->3个等子->3个等子

但是有时候你只需要得到一对元素,它们不像一个图书馆那样存储着书{title,author,number of pages}。我的XML看起来像:

<?xml version="1.0" encoding="UTF-8" ?>
<A>
    <Crap1>
        <CrapSon>1</CrapSon>
    </Crap1>
    <B>
        <Crap2>Store</Crap2>
        <C>
            <Type>N</Type>
            <D>
                <InterestingData1>Data</InterestingData1>
            </D>
            <InterestingData2>More Data</InterestingData2>
        </C>
    </B>
</A>

1.
商场
N
资料
更多数据

当然,我可以迭代所有内容,最终获得所需的数据元素。但是,有没有一种快速的方法可以访问您想要的元素,而无需重复树,只需按名称并让它搜索?

如果您不需要DOM,我想您应该使用带有处理程序的SAX解析器来处理
InterestingData1
InterestingData2

您可以使用JavaSE5和更高版本中的
javax.xml.xpath
库,并使用xpath查询文档中所需的数据

示例


您可以尝试以下方法:

import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

class Test {    

    public static void main(String args[]) throws ParserConfigurationException, SAXException, IOException{
        String xmlString ="<A>" +
                "<Crap1><CrapSon>1</CrapSon>" +
                "</Crap1>" +
                "<B>" +
                "<Crap2>Store</Crap2><C><Type>N</Type><D><InterestingData1>Data</InterestingData1></D><InterestingData2>More Data</InterestingData2></C>" +
                "</B>" +
                "</A>";

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();      
        DocumentBuilder docBuilder = dbf.newDocumentBuilder();
        Document doc = docBuilder.parse(new InputSource(new StringReader(xmlString)));

        NodeList nodelist = doc.getElementsByTagName("InterestingData2");
        NodeList nodelist2 = doc.getElementsByTagName("Type");

        String str = nodelist.item(0).getTextContent();
        String str2 = nodelist2.item(0).getTextContent();

        System.out.println("InterestingData2: "+str);
        System.out.println("Type: " + str2);
    }
}
import java.io.IOException;
导入java.io.StringReader;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入javax.xml.parsers.parserConfiguration异常;
导入org.w3c.dom.Document;
导入org.w3c.dom.NodeList;
导入org.xml.sax.InputSource;
导入org.xml.sax.SAXException;
类测试{
公共静态void main(字符串args[])抛出ParserConfiguration异常、SAXException、IOException{
字符串xmlString=“”+
"1" +
"" +
"" +
“StorenDamore数据”+
"" +
"";
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder=dbf.newDocumentBuilder();
documentdoc=docBuilder.parse(新的InputSource(新的StringReader(xmlString));
NodeList NodeList=doc.getElementsByTagName(“兴趣数据2”);
NodeList nodelist2=doc.getElementsByTagName(“类型”);
String str=nodelist.item(0.getTextContent();
字符串str2=nodelist2.item(0.getTextContent();
System.out.println(“利息数据2:+str”);
System.out.println(“类型:+str2”);
}
}
输出:

有趣的数据2:更多数据

类型:N