JAXB似乎是重新编码新行的char [英] JAXB seems to be re-encoding new line char

查看:121
本文介绍了JAXB似乎是重新编码新行的char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码...

  public static final String NEWLINE = String.format(%n) ; 
...
StringBuilder b = new StringBuilder();
...
在b.append(新行);
return b.toString();

然而,当我在SOAPUI中运行它并查看RAW时,它看起来像是再次增加它新行变成

 & #xD; 

所以而不是

 < SOAPEnvelope> 1,name,value 
2,name,value< SOAPEnvelope>

它显示为...

 <的SoapEnvelope大于1,名称,值&安培; #xD; 
2,name,value& #xD;< SOAPEnvelope>

所以当我解析使用正则表达式查询替换为空字符串时,全文仍然在



现在我不熟悉编码,但这是SOAP消息的有效载荷。信封被剥离,这是ouputed到CSV文件。当我在Notepad ++中打开CSV时,新的行char似乎工作,但编码的值仍然存在。有没有人知道我失踪了?



我尝试添加(sb = Stringbuilder)

  sb.append( <![CDATA [); 
...
sb.append(]]>);

但这似乎不起作用,JAXB似乎也逃避了这些字符。

 <?xml version =1.0encoding =UTF-8?>< soapenv:Envelope xmlns:soapenv =http ://schemas.xmlsoap.org/soap/envelope/>< soapenv:Body>< ns4:ReportResult xmlns:ns2 =http://me.com/v1_9/servicexmlns:ns3 =http ://me.com/v1_9/service/objectsxmlns:ns4 =http://me.com/v1_9/service/operations>& lt;![CDATA [000000,LK20130930120048,Compant,Data, 20130930120048 ,,&安培; #xD; 
000001,user,Fname,Mname,Lname,12345地址1,地址2222,setAddress3,setCity,OH,US,44332,N,N,E ,,,,,,,,,,,,,,,,,,
999999]]& gt< / ns4:ReportResult>< / soapenv:Body>< / soapenv:Envelope>

另外我也会像这个,但是我使用默认的编组器为JAX-WS。

解决方案

JAXB(JSR-222)实现将执行以下操作: / p>


  • \\\
    将作为新行写入XML。 >
  • \r 将被转义为& #xD;



演示代码



Foo

  import javax.xml.bind.annotation.XmlRootElement; 

@XmlRootElement
public class Foo {

public String slashN;
public String slashR;
public String slashRslashN;

}

演示

  import javax.xml.bind。*; 

public class Demo {

public static void main(String [] args)throws异常{
JAXBContext jc = JAXBContext.newInstance(Foo.class);

Foo foo = new Foo();
foo.slashN =Hello\\\
World;
foo.slashR =Hello\rWorld;
foo.slashRslashN =Hello\r\\\
World;

Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
marshaller.marshal(foo,System.out);
}

}

输出

 <?xml version =1.0encoding =UTF-8standalone = ? 是 > 
< foo>
< slashN> Hello
World< / slashN>
< slashR> Hello& #xD; World< / slashR>
< slashRslashN> Hello& #xD;
World< / slashRslashN>
< / foo>


I have the following code...

public static final String NEWLINE = String.format("%n");
...
StringBuilder b = new StringBuilder();
...
b.append(NEWLINE);
return b.toString();

However when I run this in SOAPUI and look at the RAW it looks like it is augmenting it again and the new line turns into

&#xD;

So instead of

<SOAPEnvelope>1,name,value
             2,name,value<SOAPEnvelope>

It shows up as...

<SOAPEnvelope>1,name,value&#xD;
             2,name,value&#xD;<SOAPEnvelope>

So when I parse using a regex query to replace with an empty string the full text is still around

Now I am not as familiar with encoding but this is the payload of a SOAP message. The envelope is stripped away and this is ouputed to a CSV file. When I open the CSV in Notepad++ the new line char appears to work but that encoded value is still there. Does anyone know what I am missing?

I tried adding a (sb = Stringbuilder)

sb.append("<![CDATA[");
...
sb.append("]]>");

But this doesn't seem to work, again JAXB seems to escape the chars.

<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ns4:ReportResult xmlns:ns2="http://me.com/v1_9/service" xmlns:ns3="http://me.com/v1_9/service/objects" xmlns:ns4="http://me.com/v1_9/service/operations">&lt;![CDATA[000000,LK20130930120048,Compant,Data,20130930120048,,&#xD;
000001,user,Fname,Mname,Lname,12345 Address 1,Address 2222,setAddress3,setCity,OH,US,44332,,N,N,E,,,,?,,&#xD;
999999]]&gt;</ns4:ReportResult></soapenv:Body></soapenv:Envelope>

Also I would be fine with something like this but I use the default marshaller for JAX-WS. So I am not exactly sure where to put it.

解决方案

A JAXB (JSR-222) implementation will do the following:

  • \n will be written to XML as a new line.
  • \r will be escaped as &#xD;

Demo Code

Foo

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Foo {

    public String slashN;
    public String slashR;
    public String slashRslashN;

}

Demo

import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Foo.class);

        Foo foo = new Foo();
        foo.slashN = "Hello\nWorld";
        foo.slashR = "Hello\rWorld";
        foo.slashRslashN = "Hello\r\nWorld";

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(foo, System.out);
    }

}

Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<foo>
    <slashN>Hello
World</slashN>
    <slashR>Hello&#xD;World</slashR>
    <slashRslashN>Hello&#xD;
World</slashRslashN>
</foo>

这篇关于JAXB似乎是重新编码新行的char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆