CSDN博客

img bluesky35

xml中字符的转义

发表于2004/6/29 10:51:00  2445人阅读

                                                                                   作者:bluesky35(蓝天)

在编写servlet时,遇到如下情况:

<web-app>

 <!-- Servlet -->
 <servlet>
  <servlet-name>servlet</servlet-name>
  <servlet-class>com.logon.src.logon</servlet-class>
  <init-param>
   <param-name>param</param-name>
   <param-value></param-value>
  </init-param>
 </servlet>

 <!-- Mapping -->
 <servlet-mapping>
  <servlet-name>servlet</servlet-name>
  <url-pattern>servlet/logon</url-pattern>
 </servlet-mapping>

</web-app>

如果在<param-value></param-value>中想使用"<"字符,<param-value><</param-value>,这样写的话,在web服务器启动并读入web.xml信息时会发生如下错误:

[INFO] Registry - -Loading registry information
[INFO] Registry - -Creating new Registry instance
[INFO] Registry - -Creating MBeanServer
[INFO] Http11Protocol - -Coyote HTTP/1.1をポート8080で初期化します
サービス Tomcat-Standalone を起動します
Apache Tomcat/4.1.27
[INFO] PropertyMessageResources - -Initializing, config='org.apache.struts.util.LocalStrings', returnNull=true
[INFO] PropertyMessageResources - -Initializing, config='org.apache.struts.action.ActionResources', returnNull=true
[INFO] PropertyMessageResources - -Initializing, config='org.apache.webapp.admin.ApplicationResources', returnNull=true
[ERROR] Digester - -Parse Fatal Error at line 11 column 18: The content of elements must consist of well-formed character data or markup. org.xml.sax.SAXParseException: The content of elements must consist of well-formed character data or markup.
 at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
 at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
 at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
 at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
 at org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source)
 at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
 at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
 at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
 at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
 at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
 at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
 at org.apache.commons.digester.Digester.parse(Digester.java:1548)
 at org.apache.catalina.startup.ContextConfig.applicationConfig(ContextConfig.java:282)
 at org.apache.catalina.startup.ContextConfig.start(ContextConfig.java:639)
 at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:243)
 at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:166)
 at org.apache.catalina.core.StandardContext.start(StandardContext.java:3568)
 at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1188)
 at org.apache.catalina.core.StandardHost.start(StandardHost.java:738)
 at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1188)
 at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:347)
 at org.apache.catalina.core.StandardService.start(StandardService.java:497)
 at org.apache.catalina.core.StandardServer.start(StandardServer.java:2190)
 at org.apache.catalina.startup.Catalina.start(Catalina.java:512)
 at org.apache.catalina.startup.Catalina.execute(Catalina.java:400)
 at org.apache.catalina.startup.Catalina.process(Catalina.java:180)
 at java.lang.reflect.Method.invoke(Native Method)
 at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:203)
并且在servlet中使用getInitParameter("param")无法取得"<"字符,那有没有办法能在xml中写"<"呢?

其实xml中要写"<",我们可以用html的标记来标识,如下:

<param-value>&lt;</param-value>

之后我们就可以在servlet中通过getInitParameter("param")读出"<"字符了,其它一些xml中的关键字也可以通过html标签读取出来,过程如下:

1.在web.xml中定义

2.在servlet端读取

0 0

相关博文

我的热门文章

img
取 消
img