JSP中XML标记的实时读取
设置:IMAC10.6.8、ApacheTomcat6.0.16、eXist数据库、JSP、Java、XML 我有一个JSP页面,用于从用户获取关键字请求并返回XML内容 根据返回给用户的标题XML标记的内容,我还希望显示从外部Java项目提取的其他内容 我的JSP用于提取XML内容JSP中XML标记的实时读取,xml,jsp,debugging,xml-parsing,runtime-error,Xml,Jsp,Debugging,Xml Parsing,Runtime Error,设置:IMAC10.6.8、ApacheTomcat6.0.16、eXist数据库、JSP、Java、XML 我有一个JSP页面,用于从用户获取关键字请求并返回XML内容 根据返回给用户的标题XML标记的内容,我还希望显示从外部Java项目提取的其他内容 我的JSP用于提取XML内容 <div class='result-container'> <% (more code here to call content from XML d
<div class='result-container'>
<%
(more code here to call content from XML database and transform)
content = AE.getPrettyModelContent("ae", username + "transformedFeatureModel");
out.println(content);
%>
</div>
我不是100%确定我想做的是可能的,而且似乎没有太多关于它的信息。如果有人能提供任何建议,我们将不胜感激 我可能不完全理解这个问题,但基于:
if(div id=“title”==France)
听起来你好像在试图解析和编辑你在这里输出的HTML:out.println(content)代码>。这是不可能的(您无法将输出HTML读回Java中)
以下是一些可能的解决办法:
将“content”保存为一个变量java变量,以便在以后的比较中进一步使用(我将简化它,只保存您想要的部分数据)-我认为在缩放时可能会出现问题
在将任何内容输出到页面之前,将所有比较和检查放在同一代码块中
使用一些AJAX来处理后面的逻辑——(使用Javascript检查页面上的某些功能,并可能调用一个控制器servlet来处理这些功能)
当您希望在页面中添加内容时,不要直接使用out.println(),而是使用StringBuffer
或其他工具来保存您正在打印的内容的副本,以便以后可以进行搜索。-可能(如果打印后不需要操作某些内容,请执行out.println
并将其写入StringBuffer
,然后可以进行搜索,请参见以下内容:
public static void main(String[] args){
StringBuffer sb = new StringBuffer();
sb.append("<div id=\"additionalData\">Italy</div> <div id=\"title\">France</div> ");
System.out.println(sb.indexOf("id=\"title\">France</div>") > -1); // output true
System.out.println(sb.indexOf("id=\"title\">Italy</div>") > -1); // outputs false
}
publicstaticvoidmain(字符串[]args){
StringBuffer sb=新的StringBuffer();
某人附加(“意大利-法国”);
System.out.println(sb.indexOf(“id=\“title\”>France”)>-1);//输出为true
System.out.println(sb.indexOf(“id=\“title\”>Italy”)>-1);//输出为false
}
我确信还有其他解决方案,但这正是我能马上想到的。希望这能有所帮助。使用转换读取XML,然后使用条件语句和附加指令,类似于:
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" xmlns:jsp="http://java.sun.com/JSP/Page">
<!--...-->
<xsl:template match="title">
<!-- Output div with ID and value-->
<div id="local-name(.)"><xsl:value-of select="."/></div>
<!-- Check for France -->
<xsl:if test="current() = 'France' ">
<!-- Call instruction using scriptlet -->
<jsp:scriptlet>out.println(myConceptFrance.search(myConceptFrance));</jsp:scriptlet>
</xsl:if>
</xsl:template>
out.println(myConceptFrance.search(myConceptFrance));
对于更复杂的逻辑,请使用。我使用了选项1的变体。然后使用indexOf尝试确定XML标记的内容。
<div class='concept-container'>
<%
Injector injector = Guice.createInjector(new GuiceInjector());
ConceptConnector myConceptFrance = injector.getInstance(ConceptConnector.class);
if (div id="title" == France)
{
out.println(myConceptFrance.search(myConceptFrance));
}
%>
</div>
An error occurred at line: 150 in the jsp file: /SimpleResponse2.jsp
div cannot be resolved
149:
150: if (div id="title" == France)
151: {
152: out.println(myConceptFrance.search(myConceptFrance));
153: }
public static void main(String[] args){
StringBuffer sb = new StringBuffer();
sb.append("<div id=\"additionalData\">Italy</div> <div id=\"title\">France</div> ");
System.out.println(sb.indexOf("id=\"title\">France</div>") > -1); // output true
System.out.println(sb.indexOf("id=\"title\">Italy</div>") > -1); // outputs false
}
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" xmlns:jsp="http://java.sun.com/JSP/Page">
<!--...-->
<xsl:template match="title">
<!-- Output div with ID and value-->
<div id="local-name(.)"><xsl:value-of select="."/></div>
<!-- Check for France -->
<xsl:if test="current() = 'France' ">
<!-- Call instruction using scriptlet -->
<jsp:scriptlet>out.println(myConceptFrance.search(myConceptFrance));</jsp:scriptlet>
</xsl:if>
</xsl:template>