Topic: 请问这种XML该怎么读?

  Print this page

1.请问这种XML该怎么读? Copy to clipboard
Posted by: focus
Posted on: 2006-07-27 13:53

<?xml version="1.0" encoding="UTF-8"?>
<Domain ConfigurationVersion="8.1.4.0" Name="default">
<Server JDBCLogFileName="myserver\logs\jdbc.log"
JDBCLoggingEnabled="true" ListenAddress="" ListenPort="8001"
Name="myserver" NativeIOEnabled="true"
ReliableDeliveryPolicy="RMDefaultPolicy" ServerVersion="8.1.5.0"
StdoutSeverityLevel="64" TransactionLogFilePrefix="./logs/">
<SSL Enabled="false" HostnameVerificationIgnored="false"
IdentityAndTrustLocations="KeyStores" Name="myserver"/>
<Log FileName=".\myserver\logs\myserver.log" Name="myserver"/>
<WebServer LogFileName="myserver\logs\access.log" Name="myserver"/>
</Server>
<JMSFileStore Directory="rmfilestore" Name="FileStore"/>
<WSReliableDeliveryPolicy DefaultRetryCount="10"
DefaultTimeToLive="60000" Name="RMDefaultPolicy" Store="FileStore"/>
<Application Name="LWDefault" Path="\SWECTool\develop\webapps"
StagingMode="nostage" TwoPhase="true">
<WebAppComponent Name="LWDefault" Targets="myserver" URI="LWDefault"/>
</Application>
<Security Name="default" PasswordPolicy="wl_default_password_policy"
Realm="wl_default_realm" RealmSetup="true"/>
<EmbeddedLDAP
CredentialEncrypted="{3DES}4YM+lPoXq4JjISwNiVMG0NWEYv1x8UisyHBjFGLKXTE=" Name="default"/>
<SecurityConfiguration
CredentialEncrypted="{3DES}nk17dpuVuTRweGdcTe0L3RV5vEe2OhrcvMlKP6A0WJhN24dEnm89DcWiFsx5s365+4ZS4jqOyJLErlLV1hET27eu7gjMGqK4"
Name="default" RealmBootStrapVersion="1"/>
<Realm FileRealm="wl_default_file_realm" Name="wl_default_realm"/>
<FileRealm Name="wl_default_file_realm"/>
<PasswordPolicy Name="wl_default_password_policy"/>
<JMSServer Name="WSStoreForwardInternalJMSServermyserver"
Store="FileStore" Targets="myserver">
<JMSQueue CreationTime="1113459071143"
JNDIName="jms.internal.queue.WSStoreForwardQueue"
JNDINameReplicated="false" Name="WSInternaljms.internal.queue.WSStoreForwardQueuemyserver"/>
<JMSQueue CreationTime="1113459071373"
JNDIName="jms.internal.queue.WSDupsEliminationHistoryQueue"
JNDINameReplicated="false" Name="WSInternaljms.internal.queue.WSDupsEliminationHistoryQueuemyserver"/>
</JMSServer>
<JDBCConnectionPool ConnLeakProfilingEnabled="true"
DriverName="oracle.jdbc.OracleDriver" Name="omeConnectionPool"
PasswordEncrypted="{3DES}SwcY3BM2OD0=" Properties="user=smile"
RemoveInfectedConnectionsEnabled="false" StatementCacheSize="0"
Targets="myserver" TestConnectionsOnCreate="true"
TestConnectionsOnReserve="true"
TestTableName="SQL SELECT 1 FROM DUAL" URL="jdbc:oracle:thinAngry192.168.0.165:1521:jxsf"/>
<Application Name="OmeDataSelect"
Path="\SWECTool\bea\user_projects\domains\default\myserver\upload"
StagingMode="nostage" TwoPhase="true">
<EJBComponent Name="OmeDataSelect" Targets="myserver" URI="OmeDataSelect.jar"/>
</Application>
<Log FileName=".\logs\default.log" Name="default"/>
<Application Name="SmilePrint" Path="\SWECTool\develop\rakurakuweb"
StagingMode="nostage" TwoPhase="true">
<WebAppComponent Name="SmilePrint" Targets="myserver" URI="SmilePrint"/>
</Application>
</Domain>

我只需要读取第45行的
URL="jdbc:oracle:thinAngry192.168.0.165:1521:jxsf"
就OK了,但是在网上查到的文章读取的XML的格式都比这个简单.
没有发现有读我这种XML的文章.
誰能给出代码?谢谢了!

2.Re:请问这种XML该怎么读? [Re: focus] Copy to clipboard
Posted by: zcjl
Posted on: 2006-07-27 15:07

使用xpath,"/Domain/JDBCConnectionPool"

使用dom4j的示例如下(假设前面的xml路径为d:/test.xml):
SAXReader reader = new SAXReader();
Document doc = reader.read(new File("D:\\test.xml"));
Element connPoolEle = (Element) doc.selectSingleNode("/Domain/JDBCConnectionPool");
String url = connPoolEle.attributeValue("URL");
System.out.println(url);

3.Re:请问这种XML该怎么读? [Re: focus] Copy to clipboard
Posted by: focus
Posted on: 2006-07-27 20:28

出现了这个问题该怎么解决啊?

java.lang.NoClassDefFoundError: org/jaxen/JaxenException
  at org.dom4j.DocumentFactory.createXPath(DocumentFactory.java:230)
  at org.dom4j.tree.AbstractNode.createXPath(AbstractNode.java:207)
  at org.dom4j.tree.AbstractNode.selectSingleNode(AbstractNode.java:183)
  at jp.co.smile.common.Department.main(Department.java:128)
Exception in thread "main"

4.Re:请问这种XML该怎么读? [Re: focus] Copy to clipboard
Posted by: focus
Posted on: 2006-07-27 21:40

谢谢大虾提示!
已搞定。
需要下载三个包:
1.dom4j-1.6.1.jar
2.jaxen-full.jar
3.saxpath.jar
然后添加到工程中去。
我的代码:

package jp.co.smile.common;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class Department {

public Department() {

}

public static String getDepartment(String departid) {

StringBuffer optionList = new StringBuffer();

//SQL文
String statement = "SELECT DEPARTMENT_NAME, DEPARTMENT_ID " +
"FROM DEPARTMENT " +
"ORDER BY DEPARTMENT_ID" ;

try {

//Oracle的连接
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url = "jdbc:oracle:thin:@192.168.0.165:1521:jxsf";

String user = "smile";
String password = "smile";
Connection conn = DriverManager.getConnection(url,user,password);
if(conn != null){

System.out.println("Connection Success"+conn);
}else{

System.out.println("Connection failed"+conn);
}

Statement stmt = conn.createStatement();

//SQL的执行
ResultSet result = stmt.executeQuery(statement);

//提交
conn.commit();

if (departid.trim().length() != 0) {

Map departTemp = new HashMap();
String departNm = null;

while (result.next()) {

departTemp.put(result.getString("DEPARTMENT_ID"), result.getString("DEPARTMENT_NAME"));

}

Iterator iteratr = departTemp.keySet().iterator();

Iterator iteratr2 = departTemp.keySet().iterator();

while (true) {

if (iteratr.next().equals(departid)) {

departNm = departTemp.get(departid).toString();
break;
}

}

optionList.append("<option value='" + departid + "'>" + departNm + "</option>");
optionList.append("<option value=''> </option>");

while (iteratr2.hasNext()) {

String deid = iteratr2.next().toString();

if (!deid.equals(departid)) {

optionList.append("<option value='" + deid + "'>" + departTemp.get(deid).toString() + "</option>");

}
}

} else {

optionList.append("<option value=''> </option>");

while (result.next()) {

optionList.append("<option value='" + result.getString("DEPARTMENT_ID") + "'>" + result.getString("DEPARTMENT_NAME") + "</option>");

}
}

conn.close();

} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}

return optionList.toString();
}

public static void main(String[] args) {
SAXReader reader = new SAXReader();
try {
Document doc = reader.read(new File("E:\\SWECTool\\bea\\user_projects\\domains\\default\\config.xml"));
Element connPoolEle = (Element) doc.selectSingleNode("/Domain/JDBCConnectionPool");
String url = connPoolEle.attributeValue("URL");
System.out.println(url);
} catch (DocumentException e) {

e.printStackTrace();
}

}

}

成功输出:jdbc:oracle:thin:@192.168.0.165:1521:jxsf
谢谢,呵呵。


{ one point for follow-up -- why }

5.Re:请问这种XML该怎么读? [Re: focus] Copy to clipboard
Posted by: zcjl
Posted on: 2006-07-28 09:54

gx问题得到解决 Smile


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923