因为老师实验报告要求,搭建服务端解析XML
下面代码实现转化XML格式也是在网上找的转化代码
输出在jsp页面以便于客户端解析是自己写的
一个类就解决了Test
package tests; //三只坚果 import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.apache.crimson.tree.XmlDocument; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.ParserConfigurationException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; import java.sql.ResultSet; import java.sql.SQLException; import java.io.FileOutputStream; import java.io.IOException; import java.io.FileNotFoundException; public class Test { static Connection conn=null; static String sql; static String url="jdbc:mysql://localhost:3306/mhf"; public static void main(String[] args){ Test test=new Test(); test.xmlShow(); } public String xmlShow() { String comps = null; try { // 链接数据库,取得数据 // Class.forName("oracle.jdbc.driver.OracleDriver");//oracle数据库 Class.forName("com.mysql.jdbc.Driver");// mysql数据库 conn = DriverManager.getConnection(url, "root", "123"); Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("select * from table_name"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.newDocument(); Element comp = doc.createElement("COMP"); while (rs.next()) { Element person = doc.createElement("PERSON"); Element deptno = doc.createElement("CITY");// 城市 deptno.appendChild(doc.createTextNode(String.valueOf(rs.getString(1)))); person.appendChild(deptno); Element dname = doc.createElement("WEATHER");// 天气 dname.appendChild(doc.createTextNode(new Test().xmlReplace(rs.getString(2)))); person.appendChild(dname); Element loc = doc.createElement("TEM");// 温度 loc.appendChild(doc.createTextNode(new Test().xmlReplace(rs.getString(3)))); person.appendChild(loc); comp.appendChild(person); } rs.close(); st.close(); conn.close(); doc.appendChild(comp); /* ((XmlDocument) doc).write(new FileOutputStream("src/weathers.xml")); */ comps = comp.toString(); System.out.println(comp);// 打印数据 System.out.println("操作成功!!!"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e1) { e1.printStackTrace(); } catch (ParserConfigurationException e2) { e2.printStackTrace(); } /* * catch (FileNotFoundException e3) { e3.printStackTrace(); } catch (IOException * e4) { e4.printStackTrace(); } */ System.out.print("!!!!!" + comps + "!!!!!"); return comps; } public String xmlReplace(String value){ //value=value.replace("&", "&"); // value=value.replace("<", "<"); // value=value.replace(">", ">"); // value=value.replace("‘", "‘"); value=value.replace( "<","<"); value=value.replace( ">",">"); value=value.replace( "‘","‘"); // value.replace("""", """); return value; } }
jsp页面
<%@page import="com.sun.xml.internal.bind.v2.schemagen.xmlschema.Import"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!-- 这个标签很重要 --> <xmp> <%@ page import="tests.*"%> <% Test te=new Test(); %> <%=te.xmlShow()%> </xmp>
转化后输出页面<>不见了后来折腾半小时加了<xmp></xmp>完美解决
三个包别忘了!!!提前祝福端午节快了!!!
原文地址:https://www.cnblogs.com/july7/p/10987057.html
时间: 2024-10-09 01:17:10