You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中如何从org.xml.sax.InputSource实例获取原始XML请求字符串

从org.xml.sax.InputSource提取原始XML内容的实现方案

注意:InputSource 包装的流默认是单向消费的,如果此前已经读取过该InputSource的流内容,流指针已到末尾,无法再读出数据,这种场景最优解是构造InputSource时就提前缓存原始XML字符串,兼顾性能和兼容性。

方案1:直接读取未消费的字符流(适配你当前的构造场景)

你是用StringReader构造的InputSource,在流未被消费的情况下直接读取字符流即可,代码示例如下:

import java.io.BufferedReader;
import java.io.StringReader;
import org.xml.sax.InputSource;

public class XmlExtractDemo {
    public static String getXmlFromInputSource(InputSource is) throws Exception {
        // 校验字符流是否存在
        if (is.getCharacterStream() == null) {
            throw new IllegalArgumentException("当前InputSource无可用字符流");
        }

        StringReader reader = (StringReader) is.getCharacterStream();
        // StringReader支持mark/reset,若后续还要使用该InputSource做解析,先标记流初始位置
        reader.mark(Integer.MAX_VALUE);

        // 拼接流内容
        BufferedReader br = new BufferedReader(reader);
        StringBuilder xmlContent = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            xmlContent.append(line).append("\n");
        }

        // 重置流指针到初始位置,不影响后续对InputSource的使用
        reader.reset();

        return xmlContent.toString().trim();
    }

    public static void main(String[] args) throws Exception {
        String xmlRequest = "<root><msg>测试内容</msg></root>";
        InputSource is = new InputSource(new StringReader(xmlRequest));
        String result = getXmlFromInputSource(is);
        System.out.println(result); // 输出和xmlRequest完全一致的内容
    }
}

方案2:通用兼容字节流+字符流的实现

如果你的InputSource有可能通过字节流InputStream构造,可以用通用方法适配两种流类型:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import org.xml.sax.InputSource;

public class UniversalXmlExtract {
    public static String extractXml(InputSource is) throws Exception {
        // 优先读取字符流
        if (is.getCharacterStream() != null) {
            StringReader reader = (StringReader) is.getCharacterStream();
            reader.mark(Integer.MAX_VALUE);
            BufferedReader br = new BufferedReader(reader);
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line).append("\n");
            }
            reader.reset();
            return sb.toString().trim();
        }
        // 字符流不存在时读取字节流
        else if (is.getByteStream() != null) {
            InputStream byteStream = is.getByteStream();
            byteStream.mark(Integer.MAX_VALUE);
            BufferedReader br = new BufferedReader(new InputStreamReader(byteStream, 
                is.getEncoding() != null ? is.getEncoding() : StandardCharsets.UTF_8.name()));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line).append("\n");
            }
            byteStream.reset();
            return sb.toString().trim();
        }
        throw new IllegalArgumentException("InputSource无可用流资源");
    }
}

如果你项目引入了commons-io工具包,可直接用IOUtils.toString()方法替换上述流拼接逻辑,简化代码。

内容的提问来源于stack exchange,提问作者Ritesh_RM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.17 08:36:08