In XML, there are some symbols as XML markup symbols, and in some specific cases, attribute values must be accompanied by these special symbols. The following is mainly to explain some of the commonly used special symbols of the processing
Example one: use of double quotes.
The double quotation marks are the beginning of the XML attribute value, and therefore cannot be used directly in the value. There are two kinds of processing methods.
A: There is no ' (single quote) ' in the attribute value, then single quote ' ' is used as the starting-end symbol for the attribute value
<add key= "IPhone" value= "apple"/> ... The property value is ("Apple").
FIX: <add key= "IPhone" value= ' "apple" '/>
B: There are ' (single quotes) and double quotes in attribute values. Such as... The property value is (' Apple ').
<add key= "IPhone" value= ""'apple"" />
The following table lists the five built-in entities for the characters used by XML tags.
Entity |
entity Reference |
meaning |
Lt |
< |
< (less than number) |
Gt |
> |
> (greater than sign) |
Amp |
& |
& ("and" character) |
APOs |
' |
' (apostrophe or single quotation mark) |
quot |
" |
"(double quotes) |
If the character may cause the XML parser to interpret the document structure incorrectly, use the entity instead of typing the character. ' and " Entity references are most commonly used in attribute values.
In order to achieve the transmission of complex data in the webservices, we often use XML format string to transmit, mainly because XML has the benefit of accessing data, cross-platform and cross-language. Take a look at the following example:
Public String Getallnewspace () {
StringBuffer toclient = new StringBuffer ("<root>");
if (null! = DataSet) {
while (Dataset.next ()) {
Toclient.append ("<User>");
Toclient.append ("<UserName>"); ! [cdata[
Toclient.append (dataset.getstring (1));
Toclient.append ("</UserName>"); //]]
Toclient.append ("<UserId>");
Toclient.append (dataset.getstring (2));
Toclient.append ("</UserId>");
Toclient.append ("</User>");
}
}
Toclient.append ("</root>");
return toclient.tostring ();
}
If the <UserName> node contains characters such as "&", "" "and" "", an error occurs when interpreting this XML.
There are two ways to resolve this:
One, the XML Document object to get the XML string back to the client, cannot directly return XmlDocument to the client, because the XML Document object in Java, the other language is not interpreted correctly, we can only return the XML string:
Public String Getallnewspace () {
Document document = Documenthelper.createdocument ();
Element root=document.addelement ("root");
if (null! = DataSet) {
while (Dataset.next ()) {
Element user=root.addelement ("user");
Element username=user.addelement ("UserName");
Username.settext (dataset.getstring (1));
Element userid=user.addelement ("UserId");
Userid.settext (dataset.getstring (2));
}
}
return Document.asxml ();
}
Second, add the DTD validation to the XML string: Add "!" in the Node Value section. [cdata[]] "
Public String Getallnewspace () {
StringBuffer toclient = new StringBuffer ("<root>");
if (null! = DataSet) {
while (Dataset.next ()) {
Toclient.append ("<User>");
Toclient.append ("<UserName> <![ Cdata[");
Toclient.append (dataset.getstring (1));
Toclient.append ("]] </UserName>");
Toclient.append ("<UserId>");
Toclient.append (dataset.getstring (2));
Toclient.append ("</UserId>");
Toclient.append ("</User>");
}
}
Toclient.append ("</root>");
return toclient.tostring ();
}
The easiest way to output XML when writing Ajax is to pull together a string
How to deal with special strings is a problem
See below two ways
Http://webdev.csdn.net/page/96ba432b-af4a-412c-9684-2935c617faeb
For me, I need to display it on an HTML page, I just need to convert the special string to an entity character.
< |
< |
Less than sign |
> |
> |
Greater than sign |
& |
& |
And |
' |
' |
Single quotation marks |
" |
" |
Double quotes |
It is clear that the struts tag Bean:write property filter= "True" is also implemented in this function
Open source code, easy to find this method Org.apache.struts.util. Responseutils.filter (String value)
The specific implementation is as follows:
public static string filter (String value)
{
if (value = = null)
return null;
Char content[] = new Char[value.length ()];
Value.getchars (0, Value.length (), content, 0);
StringBuffer result = new StringBuffer (content.length + 50);
for (int i = 0; i < content.length; i++)
Switch (Content[i])
{
Case://' < '
Result.append ("<");
Break
Case://' > '
Result.append (">");
Break
Case://' & '
Result.append ("&");
Break
Case 34://' "'
Result.append (""");
Break
Case 39://' \ '
Result.append ("& #39;");
Break
Default
Result.append (Content[i]);
Break
}
return result.tostring ();
}
Of course, sometimes writing JavaScript also requires a flat-out HTML, and then use a obj.innerhtml= to show your page
Special characters also need to be handled at this time.
I also follow the bean:write tag to write a very native JS version of the filter method bar
function Filter (v)
{
if (v = = null| | v== "")
Return "";
var result= "";
for (var i = 0; i < v.length; i++)
Switch (V.charat (i))
{
Case "<": result+= "<"; Break
Case ">": result+= ">"; Break
Case "&": result+= "&"; Break
Case "\" ": result+=" " "; Break
Case "'": result+= "& #39;"; Break
Default:result+=v.charat (i); break;
}
return result;
}
If you use the prototype frame, there's a way.
Escapehtml:function () {
Return This.replace (/&/g, ' & '). Replace (/</g, ' < '). Replace (/>/g, ' > ');
}
The corresponding method for converting back is unescapehtml
For example
var temp= "<>/";
Alert (temp.escapehtml ());