XML Special Character processing

Source: Internet
Author: User
Tags cdata html page string back tostring xml attribute xml parser stringbuffer

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= "&quot;&apos;apple&quot;" />

The following table lists the five built-in entities for the characters used by XML tags.

Entity entity Reference meaning

Lt

&lt;

< (less than number)

Gt

&gt;

> (greater than sign)

Amp

&amp;

& ("and" character)

APOs

&apos;

' (apostrophe or single quotation mark)

quot

&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. &apos; and &quot; 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.

&lt; < Less than sign
&gt; > Greater than sign
&amp; & And
&apos; ' Single quotation marks
&quot; " 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 ("&lt;");
Break

Case://' > '
Result.append ("&gt;");
Break

Case://' & '
Result.append ("&amp;");
Break

Case 34://' "'
Result.append ("&quot;");
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+= "&lt;"; Break
Case ">": result+= "&gt;"; Break
Case "&": result+= "&amp;"; Break
Case "\" ": result+=" &quot; "; 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, ' &amp; '). Replace (/</g, ' &lt; '). Replace (/>/g, ' &gt; ');
}

The corresponding method for converting back is unescapehtml

For example

var temp= "<>/";

Alert (temp.escapehtml ());

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.