我可以强制JAXB不要转换“进入& quot;,例如,当编组到XML? [英] Can I force JAXB not to convert " into ", for example, when marshalling to XML?

查看:138
本文介绍了我可以强制JAXB不要转换“进入& quot;,例如,当编组到XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用JAXB编组为XML的Object。一个元素包含一个包含引号()的字符串。生成的XML具有& quot; 其中存在。



即使这通常是首选,我也需要输出匹配传统系统。如何强制JAXB不转换HTML实体?



-



感谢您的回复。但是,我从未看到调用处理程序escape()。你能看一看,看看我做错了什么吗?谢谢!

  package org.dc.model; 

import java.io.IOException;
import java.io.Writer;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.dc.generated.Shiporder;

import com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler;

public class PleaseWork {
public void prettyPlease()throws JAXBException {
Shiporder shipOrder = new Shiporder();
shipOrder.setOrderid(订单ID);
shipOrder.setOrderperson(女人说,\你是怎么做的东西?\);

JAXBContext context = JAXBContext.newInstance(org.dc.generated);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE);
marshaller.setProperty(CharacterEscapeHandler.class.getName(),
new CharacterEscapeHandler(){
@Override
public void escape(char [] ch,int start,int length,
boolean isAttVal,Writer out)抛出IOException {
out.write(called escape for characters =+ ch.toString());
}
});
marshaller.marshal(shipOrder,System.out);
}

public static void main(String [] args)抛出异常{
new PleaseWork()。prettyPlease();
}
}

-



输出为:

 <?xml version =1.0encoding =UTF-8 standalone =yes?> 
< shiporder orderid =订单ID>
< orderperson>女人说,& quot; ya doin& amp;东西&安培; QUOT;< / orderperson>
< / shiporder>

如您所见,回调永远不会显示。 (一旦我收到回叫,我会担心它实际上做了我想要的。)



-

解决方案

解决方案我的队友发现:

  PrintWriter printWriter = new PrintWriter( new FileWriter(xmlFile)); 
DataWriter dataWriter = new DataWriter(printWriter,UTF-8,DumbEscapeHandler.theInstance);
marshaller.marshal(request,dataWriter);

不要将xmlFile传递给marshal(),而是传递知道编码和适当的DataWriter转义处理程序,如果有的话。



注意:由于DataWriter和DumbEscapeHandler都在com.sun.xml.internal.bind.marshaller包中,因此必须引导javac。 / p>

I have an Object that is being marshalled to XML using JAXB. One element contains a String that includes quotes ("). The resulting XML has &quot; where the " existed.

Even though this is normally preferred, I need my output to match a legacy system. How do I force JAXB to NOT convert the HTML entities?

--

Thank you for the replies. However, I never see the handler escape() called. Can you take a look and see what I'm doing wrong? Thanks!

package org.dc.model;

import java.io.IOException;
import java.io.Writer;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.dc.generated.Shiporder;

import com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler;

public class PleaseWork {
    public void prettyPlease() throws JAXBException {
        Shiporder shipOrder = new Shiporder();
        shipOrder.setOrderid("Order's ID");
        shipOrder.setOrderperson("The woman said, \"How ya doin & stuff?\"");

        JAXBContext context = JAXBContext.newInstance("org.dc.generated");
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.setProperty(CharacterEscapeHandler.class.getName(),
                new CharacterEscapeHandler() {
                    @Override
                    public void escape(char[] ch, int start, int length,
                            boolean isAttVal, Writer out) throws IOException {
                        out.write("Called escape for characters = " + ch.toString());
                    }
                });
        marshaller.marshal(shipOrder, System.out);
    }

    public static void main(String[] args) throws Exception {
        new PleaseWork().prettyPlease();
    }
}

--

The output is this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<shiporder orderid="Order's ID">
    <orderperson>The woman said, &quot;How ya doin &amp; stuff?&quot;</orderperson>
</shiporder>

and as you can see, the callback is never displayed. (Once I get the callback being called, I'll worry about having it actually do what I want.)

--

解决方案

Solution my teammate found:

PrintWriter printWriter = new PrintWriter(new FileWriter(xmlFile));
DataWriter dataWriter = new DataWriter(printWriter, "UTF-8", DumbEscapeHandler.theInstance);
marshaller.marshal(request, dataWriter);

Instead of passing the xmlFile to marshal(), pass the DataWriter which knows both the encoding and an appropriate escape handler, if any.

Note: Since DataWriter and DumbEscapeHandler are both within the com.sun.xml.internal.bind.marshaller package, you must bootstrap javac.

这篇关于我可以强制JAXB不要转换“进入&amp; quot;,例如,当编组到XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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