编码特殊的HTML字符< > /“在Java中 [英] Encoding special HTML characters < > / " in Java
问题描述
我正在使用Java 1.6.0
我正在寻找用Java编码特殊HTML字符的正确方法
My HTML
< div id =sliceXML> Florida< DIV>
我试图定义一个String xmlMatch作为内容这个案例佛罗里达州)的上述div标签通过使用下面的Java。不过,我相信我没有正确定义我的startTag或endTag。
My Java
字符串testContent = contentPara;
String startTag =\sliceXML\>;
String endTag =< / div;
String xmlMatch = null;
int startPosition = testContent.indexOf(startTag);
if(startPosition> 1){
int subStringIndex = startPosition + startTag.length();
int endPosition = testContent.indexOf(endTag,subStringIndex);
if(endPosition> = startPosition){
xmlMatch = testContent.substring(subStringIndex,endPosition);
out.println(xmlMatch.length());
//out.println (startTag);
out.println(Florida.equals(xmlMatch));
out.println(florida.equals(xmlMatch));
}
}
任何帮助都非常有用。这也可以让我回答以前的相关问题此处
编辑
$ b 解决方案 p>
正如我在下面解释的,我相信我的问题是在 String endTag =< / div;
为了解决这个问题,我简单地将我的结束标记更改为 String endTag =<;
I仍然不知道为什么会发生这种情况,如果有人能够回答,那将会很棒。
我真的会使用HTML解析器,比如容易混淆的
p>它可以让你免于头痛,比如解析和处理字符实体和编码。
I am using Java 1.6.0
I am looking for the correct methods of encoding special HTML characters in Java
My HTML
<div id="sliceXML">Florida</div>
I am trying to define a String xmlMatch as the content (in this case Florida) of the above div tag by using the below Java. However i believe i do not have my startTag or endTag defined correctly.
My Java
String testContent = contentPara;
String startTag = "\"sliceXML\">";
String endTag = "</div";
String xmlMatch = null;
int startPosition = testContent.indexOf(startTag);
if(startPosition >1){
int subStringIndex = startPosition + startTag.length();
int endPosition = testContent.indexOf(endTag, subStringIndex);
if(endPosition >= startPosition){
xmlMatch = testContent.substring(subStringIndex, endPosition);
out.println(xmlMatch.length());
//out.println(startTag);
out.println("Florida".equals(xmlMatch));
out.println("florida".equals(xmlMatch));
}
}
Any help is much appricated. This would also allow me to answer a previous related question here
EDIT
WORK AROUND SOLUTION
As i explain below, i believe my issue was with the forward slash in String endTag = "</div";
To get past this problem i simply changed my end tag to String endTag = "<";
I still dont know why this happened, it would be great if someone could answer.
I would really use an HTML parser, such as the confusingly-named JTidy (it's an HTML pretty-printer, but also gives you a DOM interface to the HTML structure).
It'll save you from headaches such as the parsing and handling character entities and encoding.
这篇关于编码特殊的HTML字符< > /“在Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!