gpt4 book ai didi

Java:实现转换 View 模式,使用 XSL 文件将 XML 转换为 HTML

转载 作者:行者123 更新时间:2023-12-02 11:36:59 25 4
gpt4 key购买 nike

我已阅读Oracle教程:https://docs.oracle.com/javase/tutorial/jaxp/xslt/transformingXML.html

w3school 指南:https://www.w3schools.com/xml/xsl_intro.asp

还有 StackOverflow 的线程:XSLT processing with Java?

我正在学习 Transform View 模式,我想用 Java 实现它。

我创建了一个 XML 文件 Alumnos.xml:

<?xml version="1.0"?>
<ALUMNO>
<NOMBRE>Luis</NOMBRE>
<APPELLIDO1>Navarro</APPELLIDO1>
<APPELLIDO2>Morote</APPELLIDO2>
<ASIGNATURAS>
<ASIGNATURA>PR4</ASIGNATURA>
<NOTA>5</NOTA>
<ASIGNATURA>BD2</ASIGNATURA>
<NOTA>8</NOTA>
<ASIGNATURA>IR</ASIGNATURA>
<NOTA>10</NOTA>
</ASIGNATURAS>
</ALUMNO>

还有一个名为 Alumnos.xsl 的 Xsl 文件

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet
xmlns:xsl=
"http://www.w3.org/1999/XSL/Transform"
version="1.0"
>

<xsl:template match="ALUMNO">
<HTML>
<BODY bgcolor="gray">
<xsl:apply-templates/>
</BODY>
</HTML>
</xsl:template>

<xsl:template match="NOMBRE">
<P>
<B>Nombre: </B>
<xsl:apply-templates/>
</P>
</xsl:template>

<xsl:template match="ASIGNATURA">
<h1>Aginatura: </h1>
<xsl:apply-templates/>
</xsl:template>

<xsl:output method="html"/>

</xsl:stylesheet>

目前我有以下 Java 类,AlumnosCommand.java:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package frontController;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

/**
*
* @author YonePC
*/
@WebServlet(name = "AlumnosCommand", urlPatterns = {"/AlumnosCommand"})
public class AlumnosCommand extends FrontCommand {

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter writer = response.getWriter();
writer.println("<h1>Hi</h1>");

}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
public String getServletInfo() {
return "Short description";
}// </editor-fold>

@Override
public void process(HttpServletRequest request) {
try {

TransformerFactory factory = TransformerFactory.newInstance();
StreamSource xls = new StreamSource(new File("Alumnos.xsl"));
Transformer newTransformer = factory.newTransformer(xls);

StreamSource xml = new StreamSource(new File("Alumnos.xml"));
newTransformer.transform(xml, new StreamResult(new File("output.xml")));

forward("/Alumnos.jsp");

} catch (ServletException ex) {
Logger.getLogger(AlumnosCommand.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(AlumnosCommand.class.getName()).log(Level.SEVERE, null, ex);
} catch (TransformerException ex) {
Logger.getLogger(AlumnosCommand.class.getName()).log(Level.SEVERE, null, ex);
}
}

}

上面的类的目的是读取 xml,并使用 xls 对其进行转换,并将其转换为新的 xml 文件(稍后应使用它来打印页面的实际 HTML)。

我面临的困难是,当我按“查看学生列表”时:

enter image description here

AlumnosCommand.java 显示完全空白:

enter image description here

我认为这可能是路由/服务器问题,但没关系,因为如果我删除与加载 xml 相关的行并将其转换,我们会看到 AlumnosCommand.java

enter image description here

如何使用给定的 XML 文件和建议的 XSL 规则,将其转换为 HTML 并将其呈现到 AlumnosCommand.java 页面中?

作为注释,我包括了应用程序的架构: enter image description here

最佳答案

process() 方法使用 Alumnos.xslAlumnos.xml 转换为 output.xml 。此 output.xml 将包含所有 HTML 代码,并将在与 XML 和 XSL 文件相同的位置创建。然后请求被转发到 Alumnos.jsp。此 JSP 文件是否执行从 output.xml 读取以打印任何内容?我的猜测是否定的,因此您在网页上看不到任何内容。

需要修改 process() 才能将转换写入 Writer 对象,例如PrintWriterHttpServletResponse 已经有一个 getWriter() 方法,该方法将有助于打印输出。下面是执行此操作的示例代码。

protected void doGet(HttpServletRequest request, HttpServletResponse response) {
try {
TransformerFactory factory = TransformerFactory.newInstance();
StreamSource xsl = new StreamSource(new File("Alumnos.xsl"));
Transformer newTransformer = factory.newTransformer(xsl);

StreamSource xml = new StreamSource(new File("Alumnos.xml"));
PrintWriter writer = response.getWriter();
Result result = new StreamResult(writer);
newTransformer.transform(xml, result);
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (TransformerException te) {
te.printStackTrace();
}
}

关于Java:实现转换 View 模式,使用 XSL 文件将 XML 转换为 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48870107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com