xml数据解析非法字符问题解决

最近项目中有用到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&#160;http://1.1.1.1:803/geoserver/schemas/wfs/1.1.0/wfs.xsd&#160;http://1.1.1.1:8080/geoserver/xxx&#160;http://1.1.1.1:803/geoserver/xxx/wfs?service=WFS&amp;amp;version=1.0.0&amp;amp;request=DescribeFeatureType&amp;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&#160;http://1.1.1.1:803/geoserver/schemas/wfs/1.1.0/wfs.xsd&#160;http://1.1.1.1:8080/geoserver/xxx&#160;http://1.1.1.1:803/geoserver/xxx/wfs?service=WFS&amp;amp;version=1.0.0&amp;amp;request=DescribeFeatureType&amp;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>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值