Java 如何用JDOM读取XML

Java 如何用JDOM读取XML,java,jdom,Java,Jdom,如何获取此标记的值 <executingChannel><mnemonic>8</mnemonic></executingChannel> 在这个XML低版本中,使用JDOM <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope"> <SOAP-ENV:Header xmlns:SOAP-ENV="http://www.w3.org/

如何获取此标记的值

<executingChannel><mnemonic>8</mnemonic></executingChannel>
在这个XML低版本中,使用JDOM

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
<SOAP-ENV:Header xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
<md:metadata xmlns:md="http://www.teste.net/cmm/services/metadata/v2.0">
    <serviceId>calculateEntityCache_Version_From_List</serviceId>
    <serviceVersion>1.0</serviceVersion>
    <institutionType>UNDEFINED</institutionType>
    <targetChannel>
    <mnemonic>8</mnemonic>
    </targetChannel>        
    <executingChannel>
    <mnemonic>8</mnemonic>
    </executingChannel>

>

在JDOM中,您可以通过以下方式将文档读入内存:

SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(source of XML ... URL, file, etc.);
Namespace md = Namespace.get("md",
         "http://www.teste.net/cmm/services/metadata/v2.0");
然后,您可以通过以下方式查询文档:

String path = "//md:metadata/executingChannel/mnemonic";
XPathExpression<Element> xp = XPathFactory.instance()
       .compile(path, Filters.element(), null, md);
Element mnemonic = xp.evaluateFirst(doc);
System.out.println(mnemonic.getText());

下面是使用XPath表达式和


getElementsByTagNameString标记名
import com.ximpleware.*;
public class simpleQuery {

    public static void main(String[] s) throws Exception{

        VTDGen vg = new VTDGen();
        if (!vg.parseFile("input.xml", true))
            return;
        VTDNav vn = vg.getNav();
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath("//targetChannel/mnemonic/text()");
        int i = ap.evalXPath();
        if (i!=-1)
            System.out.println(" result ==>"+vn.toString(i));
    }
}