2015-03-02 88 views
1

如何用JDOM如何读取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> 

>

+0

的getElementsByTagName(字符串标记名)检索使用XPath表达式的值的代码 – 2015-03-02 15:03:25

回答

0

在JDOM,以获得该代码

<executingChannel><mnemonic>8</mnemonic></executingChannel> 

的价值在这个XML低,阅读文档以记忆:

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"); 

然后,你可以将与ERY文档:

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()); 
0

这里是与vtd-xml

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)); 
    } 
}