【Java开源代码栏目提醒】:网学会员,鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“xmlRestaurantRemoting.java”一文,供大家参考学习!
package flashRemoting.
xml;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
import org.w3c.dom.*;
import org.apache.xerces.dom.*;
public class xmlRestaurantRemoting {
private static Document ReturnXML;
public Document GetHotelInfo(Document Doc) {
//获得Root元素
Element rt = Doc.getDocumentElement();
//获得Root元素的子节点
列表 NodeList nlst = rt.getChildNodes();
//获得子节点属性映射
NamedNodeMap nnm = Doc.getFirstChild().getAttributes();
//取得参数
String selectedHotel_name = nnm.getNamedItem("hotel_name").getNodeValue();
System.out.println(selectedHotel_name);
System.out.println(nnm.item(0).getNodeValue() + nnm.item(1).getNodeValue() +
nnm.item(2).getNodeValue());
String selectedHotel_flavor = nnm.getNamedItem("hotel_flavor").getNodeValue();
System.out.println(selectedHotel_flavor);
String selectedHotel_location = nnm.getNamedItem("hotel_location").
getNodeValue();
System.out.println(selectedHotel_location);
String sql = "select * from main WHERE Hotel_name LIKE ? OR Hotel_flavor LIKE ? OR Hotel_location LIKE ?";
Connection connection = null;
PreparedStatement stmt = null;
ResultSet rs = 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);
stmt.setObject(1, selectedHotel_name);
stmt.setObject(2, selectedHotel_flavor);
stmt.setObject(3, selectedHotel_location);
rs = stmt.executeQuery();
System.out.println("使用DOMImplementationImpl创建新的DOM文档");
DOMImplementation domImpl = DOMImplementationImpl.getDOMImplementation();
System.out.println("创建根节点");
ReturnXML = domImpl.createDocument(null, "list", null);
System.out.println("创建根节点");
Element root = ReturnXML.getDocumentElement();
//ReturnXML.appendChild(root);
while (rs.next()) {
//获取字段值
String hotel_flavor = rs.getString("hotel_flavor");
String hotel_name = rs.getString("hotel_name");
String hotel_level = rs.getString("hotel_level");
String hotel_location = rs.getString("hotel_location");
String hotel_phone = rs.getString("hotel_phone");
//添加子节点
System.out.println("在根节点下创建restaurant节点");
Element restaurantNode = ReturnXML.createElement("restaurant");
root.appendChild(restaurantNode);
System.out.println("在restaurant节点下创建hotel_name属性并赋值");
restaurantNode.setAttribute("hotel_name", hotel_name);
System.out.println("在restaurant节点下使用restaurantNode元素创建flavor节点");
Element flavorNode = ReturnXML.createElement("flavor");
//将该节点转化成 restaurant对象的子节点
restaurantNode.appendChild(flavorNode);
//在DOM文档中增加一个Text节点
Text flavorName = ReturnXML.createTextNode(hotel_flavor);
System.out.println("为flavor节点定义文本节点");
flavorNode.appendChild(flavorName);
System.out.println("在restaurant节点下使用restaurantNode元素创建level节点");
Element levelNode = ReturnXML.createElement("level");
//将该节点转化成 restaurant对象的子节点
restaurantNode.appendChild(levelNode);
//在DOM文档中增加一个Text节点
Text levelName = ReturnXML.createTextNode(hotel_level);
System.out.println("为level节点定义文本节点");
levelNode.appendChild(levelName);
System.out.println("在restaurant节点下使用restaurantNode元素创建location节点");
Element locationNode = ReturnXML.createElement(