lucene-解析xml

1、安装DOM4j
http://www.dom4j.org/
2、安装jaxen
http://jaxen.org/releases.html
3、代码
package extract;
import java.io.*;
import org.dom4j.*;
import org.dom4j.io.*;
import java.util.*;
public class XmlExtract {
private SAXReader reader;
private Document document;

/**
* @param args
*/
public XmlExtract(){
reader=new SAXReader();
try {
document=reader.read(new File("./htmls/abcde.xml"));
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public void exxml(String path){ 
if (document==null) return; 
List l=document.selectNodes(path);
System.out.println(path);
Iterator it=l.iterator();
while (it.hasNext()){
Element ele=(Element) it.next();
List li=ele.elements();
Iterator i=li.iterator();
while (i.hasNext()){
Element e=(Element) i.next();
System.out.println("name:"+e.getName()+"\t text:"+e.getText());
}

}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
XmlExtract xmlext=new XmlExtract(); 
xmlext.exxml("/Config/vmacore/threadPool"); 
}
}

4、效果与截图
/Config/vmacore/threadPool
name:TaskMax text:40
name:IoMax text:81
name:IoMin text:1
xml文件内容
<!-- Sample configuration file for running UFA stack for P2V and VMI -->
<Config>
<vmacore>
<threadPool>
<TaskMax>40</TaskMax>
<IoMax>81</IoMax>
<IoMin>1</IoMin>
</threadPool>
<impersonate>true</impersonate>
<!--
<useRefTracker>true</useRefTracker>
<printRefDetails>true</printRefDetails>
-->
</vmacore>
<plugins>
<ufa_slave>
<path>ufa-slave.dll</path>
<primarySnapshotDriver>vss</primarySnapshotDriver>
<alternativeSnapshotDriver>vsnap</alternativeSnapshotDriver>
<enableBlockLevelVolumeCloning>true</enableBlockLevelVolumeCloning>
<vstor2Instance>vstor2-ws60</vstor2Instance>
</ufa_slave>
<ufa_client>
<path>ufa-client.dll</path>
</ufa_client>
<ufa_sysReconfig>
<path>ufa-sysReconfig.dll</path>
</ufa_sysReconfig>
<ufa_sysMigration>
<path>ufa-sysMigration.dll</path>
</ufa_sysMigration>
<ufa_vmImporter>
<path>ufa-vmImporter.dll</path>
<deleteVmFromFailedClone>true</deleteVmFromFailedClone>
</ufa_vmImporter>
<ufa_agent>
<path>ufa-agent.dll</path>
<enableRemoteAccess>true</enableRemoteAccess>
<enableSoapAdapter>false</enableSoapAdapter>
<!--<soapPort>8086</soapPort>-->
<enableVmdbAdapter>true</enableVmdbAdapter>
<!--<vmdbAdapterListener>both</vmdbAdapterListener>-->
<!--<vmdbAdapterListener>tcp</vmdbAdapterListener>-->
<vmdbAdapterListener>namedPipe</vmdbAdapterListener>
<vmdbPipeName>vmware-ufad-ws60-beta3-vmdb</vmdbPipeName>
<!--<vmdbPort>9006</vmdbPort>-->
</ufa_agent>
</plugins>
<log>
<name>vmware-converter</name>
<level>verbose</level>
</log>
<!-- Remove the following node to disable SSL -->
<ssl>
<!-- Private key file -->
<privateKey>ssl/rui.key</privateKey>
<!-- Certificate file -->
<certificate>ssl/rui.crt</certificate>
</ssl>
</Config>

你可能感兴趣的:(xml,Lucene)