最近项目中有用到xml作为服务参数,请求服务,老是报参数错误,提示有非法字符。
xml结构大致如下:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<wfs:Transaction version="2.0.2" service="WFS"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:wfs="http://www.opengis.net/wfs"
xmlns:gml="http://www.opengis.net/gml"
xmlns:fes="http://www.opengis.net/fes/2.0"
xmlns:xxx="http://1.1.1.1:8080/geoserver/xxx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/wfs http://1.1.1.1:803/geoserver/schemas/wfs/1.1.0/wfs.xsd http://1.1.1.1:8080/geoserver/xxx http://1.1.1.1:803/geoserver/xxx/wfs?service=WFS&amp;version=1.0.0&amp;request=DescribeFeatureType&amp;typeName=xxx%3Axxx_edit">
<wfs:Insert>
<xxx:xxx_edit gml:id="xxx_edit.0">
<xxx:gdgc>12</xxx:gdgc>
<xxx:sbzt>测试</xxx:sbzt>
<xxx:ejglgs>0</xxx:ejglgs>
<xxx:sccj>0</xxx:sccj>
</xxx:xxx_edit>
</wfs:Insert>
</wfs:Transaction>
目测没有非法字符,也可能是隐藏了,去除非法字符。
// 保留合法字符
public String stripNonValidXMLCharacters(String in) {
StringBuffer out = new StringBuffer(); // Used to hold the output.
char current; // Used to reference the current character.
if (in == null || ("".equals(in))) return ""; // vacancy test.
for (int i = 0; i < in.length(); i++) {
current = in.charAt(i); // NOTE: No IndexOutOfBoundsException caught here; it should not happen.
if ((current == 0x9) ||
(current == 0xA) ||
(current == 0xD) ||
((current >= 0x20) && (current <= 0xD7FF)) ||
((current >= 0xE000) && (current <= 0xFFFD)) ||
((current >= 0x10000) && (current <= 0x10FFFF)))
out.append(current);
}
return out.toString();
}
还是不行,还是报一样的错误,非法的空字符。猜想有没有可能是xml编码的问题,改下xml编码。
encoding='gbk',解决。
<?xml version='1.0' encoding='gbk' standalone='yes' ?>
<wfs:Transaction version="2.0.2" service="WFS"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:wfs="http://www.opengis.net/wfs"
xmlns:gml="http://www.opengis.net/gml"
xmlns:fes="http://www.opengis.net/fes/2.0"
xmlns:xxx="http://1.1.1.1:8080/geoserver/xxx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/wfs http://1.1.1.1:803/geoserver/schemas/wfs/1.1.0/wfs.xsd http://1.1.1.1:8080/geoserver/xxx http://1.1.1.1:803/geoserver/xxx/wfs?service=WFS&amp;version=1.0.0&amp;request=DescribeFeatureType&amp;typeName=xxx%3Axxx_edit">
<wfs:Insert>
<xxx:xxx_edit gml:id="xxx_edit.0">
<xxx:gdgc>12</xxx:gdgc>
<xxx:sbzt>测试</xxx:sbzt>
<xxx:ejglgs>0</xxx:ejglgs>
<xxx:sccj>0</xxx:sccj>
</xxx:xxx_edit>
</wfs:Insert>
</wfs:Transaction>