Java 如何将XML文本作为输入流获取

Java 如何将XML文本作为输入流获取,java,jaxb,inputstream,stax,vtd-xml,Java,Jaxb,Inputstream,Stax,Vtd Xml,我有一个客户,我正在使用它来调用Jersey REST服务 public class JerseyClient { public static void main(String[] args) { ClientConfig config = new DefaultClientConfig(); Client client = Client.create(config); WebResource service = c

我有一个客户,我正在使用它来调用Jersey REST服务

public class JerseyClient {
    public static void main(String[] args) {
        ClientConfig config   = new DefaultClientConfig();
        Client       client   = Client.create(config);
        WebResource  service  = client.resource(getBaseURI());
        String       response = service.accept(MediaType.TEXT_XML)
                                       .header("Content-Type", "text/xml; charset=UTF-8")
                                       .entity(new File("E:/postx.xml"))
                                       .post(String.class);

        System.out.println(response);
    }

    private static URI getBaseURI() {
        return UriBuilder.fromUri("http://localhost:8080/MyService/rest/xmlServices/doSomething").build();
    }
}
目前在服务器端,我有以下功能:

@POST
    @Path("/doSomething")
    @Consumes(MediaType.TEXT_XML)
    @Produces(MediaType.TEXT_XML)
    public Response consumeXMLtoCreate(ProcessJAXBObject jaxbObject) {
如何更改上述服务器端代码,以便使用stAX将一个特定元素流式传输到磁盘,而不是将所有对象转换为内存。我的目标是将这个包含二进制编码数据的元素流式传输到磁盘

我收到的有效载荷如下所示:

<?xml version="1.0" encoding="utf-8"?> <ProcessRequest> <DeliveryDate>2015-12-13</DeliveryDate>  <AttachmentBinary>iVBORw0KGgoAAAANSUhEUgAAAFoA</AttachmentBinary> <AttachmentBinary>iVBORw0KGgoAAAANSUhEUgAAAFoA</AttachmentBinary> </ProcessRequest>
客户端我有这样一个:

File file = new File("E:/postx.xml");
        FileInputStream fileInStream = null;

            fileInStream = new FileInputStream(file);

        ClientConfig config   = new DefaultClientConfig();
        Client       client   = Client.create(config);
        WebResource  service  = client.resource(getBaseURI());

         String tempImage = myfile.jpg;

        String sContentDisposition = "attachment; filename=\"" + tempImage+"\"";

        ClientResponse response = service.type(MediaType.APPLICATION_OCTET_STREAM)
                                       .header("Content-Disposition", sContentDisposition)
                                       .post(ClientResponse.class, fileInStream);
我对这个解决方案有一些疑问,首先,如果堆中有一个字符串对象需要解码,那么如何避免同样的内存问题

第二,当我移除图像元素后,我是否可以使用vtd xml重新构建一个对象或输入流,因为我想使用JAXB进行处理


我相信XMLModifier的remove()方法应该允许我使用某种XML表示减去我现在写入磁盘的元素。

我假设您知道如何在代码中与HTTP协议交互。因此您知道如何将输入流中的字节读入字节缓冲区。。。下面的代码将从此点开始接管

public void readBinaryAttachment(HTTPInputStream input) throws VTDException, IOException{
// first read xml bytes into XMLBytes
....
VTDGen vg = new VTDGen();
vg.setDoc(XMLBytes);
vg.parse(false);//
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("/ProcessRequest/BinaryAttachment/text()");
int i=0;
while((i=ap.evalXPath())!=-1){
   //i points to text node of 
   String s = vn.toRawString(i);
  // you need to decode them 
}

}

@vtd xml作者STaX不是必需的。如果您能从传入的请求中提供有关如何实现vtd xml的任何信息,我将不胜感激。如果您能通过包含一些xml来扩展您的问题,或许我可以插入一些代码片段?@vtd xml作者谢谢,我刚刚添加了一个xmlsnippet@vtd-xml作者:stax如何将xml转换为“各种临时”对象?字符串对象,字节数组对象和属性对象。。。下面发生的各种事情都会将这些事件发送到您的应用程序逻辑中。我已经根据您的建议,用基于基本工作示例的代码更新了我的问题,尽管我还有更多的问题,关于如何在Jersey中使用JAXB。
public void readBinaryAttachment(HTTPInputStream input) throws VTDException, IOException{
// first read xml bytes into XMLBytes
....
VTDGen vg = new VTDGen();
vg.setDoc(XMLBytes);
vg.parse(false);//
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("/ProcessRequest/BinaryAttachment/text()");
int i=0;
while((i=ap.evalXPath())!=-1){
   //i points to text node of 
   String s = vn.toRawString(i);
  // you need to decode them 
}

}