【Java开源代码栏目提醒】:网学会员鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“xmlRestaurantRemoting_jdom.java”一文,供大家参考学习
package flashRemoting.
xml;
//使用jdom处理XML并返回给Flash Remoting MX网关
import java.sql.*;
import java.util.*;
import javax.sql.*;
import javax.naming.*;
import org.jdom.*;
import org.jdom.output.*;
import java.io.IOException;
public class xmlRestaurantRemoting_jdom {
private Document ReturnXML;
private org.w3c.dom.Document DOMReturnXML;
public org.w3c.dom.Document GetHotelInfoAll() {
String sql = "select * from main";
Connection connection = null;
PreparedStatement stmt = null;
ResultSet myRs = null;
System.out.println("建立连接,开始查询数据库");
try {
String dsName="restaurant";
Context ctx = (Context) new InitialContext().lookup("java:comp/env");
DataSource ds = (DataSource) ctx.lookup(dsName);
connection = ds.getConnection();
stmt = connection.prepareStatement(
sql);
myRs = stmt.executeQuery();
System.out.println("使用jdom创建新的
文档");
ReturnXML = new Document(new Element("list"));
while (myRs.next()) {
//获取字段值
String hotel_flavor = myRs.getString("hotel_flavor");
String hotel_name = myRs.getString("hotel_name");
String hotel_level = myRs.getString("hotel_level");
String hotel_location = myRs.getString("hotel_location");
String hotel_phone = myRs.getString("hotel_phone");
//添加子节点
System.out.println("在根节点下创建restaurant节点");
Element restaurantNode = new Element("restaurant");
ReturnXML.getRootElement().addContent(restaurantNode);
restaurantNode.setAttribute("hotel_name", hotel_name);
System.out.println("在restaurant节点下使用restaurantNode元素创建flavor节点");
Element flavorNode = new Element("flavor").setText(hotel_flavor);
restaurantNode.addContent(flavorNode);
System.out.println("在restaurant节点下使用restaurantNode元素创建level节点");
Element levelNode = new Element("level").setText(hotel_level);
restaurantNode.addContent(levelNode);
System.out.println("在restaurant节点下使用restaurantNode元素创建location节点");
Element locationNode = new Element("location").setText(hotel_location);
restaurantNode.addContent(locationNode);
System.out.println("在restaurant节点下使用restaurantNode元素创建phone节点");
Element phoneNode = new Element("phone").setText(hotel_phone);
restaurantNode.addContent(phoneNode);
//下面的用于测试目的
System.out.println(ReturnXML.getRootElement().getChildren("restaurant").
toString());
System.out.println(ReturnXML.getRootElement().getChildren("restaurant").
size());
System.out.println(restaurantNode.getAttributeValue("hotel_name"));
System.out.println(restaurantNode.getText());
System.out.println(restaurantNode.getChildText("phone"));
System.out.println(ReturnXML.getRootElement().getChildren("restaurant").
get(0).toString());
}
}
catch (Exception e) {
System.out.println(e);
}
finally {
try {
myRs.close();
stmt.close();
connection.close();
}
catch (Exception e) {}
}
//注意,JDOM的Document类型,Flash Remoting MX的DOM适配器并不能识别
//因此,必须转化成org.w3c.dom.Document
DOMOutputter DOMOutput = new DOMOutputter();
try {
DOMReturnXML = DOMOutput.output(ReturnXML);
}
catch (Exception e) {
System.out.println(e);
}
return DOMReturnXML;
}
public org.w3c.dom.Document GetHotelInfo(org.w3c.dom.Document Doc) {
//注意,参数也必须是org.w3c.dom.Document类型
//获得Root元素
org.w3c.dom.Element rt = Doc.getDocumentElement();
//获得Root元素的子节点
列表 org.w3c.dom.NodeList nlst = rt.getChildNodes();
//获得子节点属性映射
org.w3c.dom.NamedNodeMap nnm = Doc.getFirstChild().