我正在尝试与JAXB进行编组.
我的输出就像
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<name><![CDATA[<h1>kshitij</h1>]]></name>
<surname><h1>solanki</h1></surname>
<id><h1>1</h1></id>
</root>
Run Code Online (Sandbox Code Playgroud)
但我需要输出像
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<name><![CDATA[<h1>kshitij</h1>]]></name>
<surname><![CDATA[<h1>solanki</h1>]]></surname>
<id><![CDATA[0]]></id>
</root>
Run Code Online (Sandbox Code Playgroud)
我正在使用以下代码来执行此操作.如果我取消注释代码我得到Property Binding Exception.没有它我可以编译,但我没有得到确切的所需输出.
package com.ksh.templates;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import com.sun.xml.bind.marshaller.CharacterEscapeHandler;
public class MainCDATA {
public static void main(String args[])
{
try
{
String name = "<h1>kshitij</h1>";
String surname = "<h1>solanki</h1>";
String id = "<h1>1</h1>";
TestingCDATA cdata = new TestingCDATA();
cdata.setId(id);
cdata.setName(name);
cdata.setSurname(surname);
JAXBContext jaxbContext = JAXBContext.newInstance(TestingCDATA.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); …Run Code Online (Sandbox Code Playgroud) 我希望转换一个看起来像这样的类......
public class Amenity {
public String id;
public String value;
}
Run Code Online (Sandbox Code Playgroud)
使用JaxB注释将以下XML转换为以下XML:
<amenity id="id-string-here">value-string-here</amenity>
Run Code Online (Sandbox Code Playgroud)
有谁知道在value成员变量上使用什么注释来完成这个?我到目前为止最接近的是:
@XmlRootElement
public class Amenity {
@XmlAttribute
public String id;
@XmlElement
public String value;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这种方法不允许我指定value不应将成员变量呈现为自己的标记<value></value>.
如何使用CDATA准备XML,
我通过Jaxb预先做出这个回应,
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<SOAP-ENV:Header/>
<soapenv:Body>
<tem:RequestData>
<tem:requestDocument>
<![CDATA[
<Request>
<Authentication CMId="68" Function="1" Guid="5594FB83-F4D4-431F-B3C5-EA6D7A8BA795" Password="poihg321TR"/>
<Establishment Id="4297867"/>
</Request>
]]>
</tem:requestDocument>
</tem:RequestData>
</soapenv:Body>
</soapenv:Envelope>
Run Code Online (Sandbox Code Playgroud)
但是从Jaxb我没有得到CDATA,如何把CDATA放在<tem:requestDocument>元素中.
这是我的Java代码:
public static String test1() {
try {
initJB();
String response = null;
StringBuffer xmlStr = null;
String strTimeStamp = null;
com.cultagent4.travel_republic.gm.Envelope envelope = null;
com.cultagent4.travel_republic.gm.Header header = null;
com.cultagent4.travel_republic.gm.Body body = null;
com.cultagent4.travel_republic.gm.RequestData requestData = null;
com.cultagent4.travel_republic.gm.RequestDocument requestDocument = null;
com.cultagent4.travel_republic.gm.RequestDocument.Request request = null;
com.cultagent4.travel_republic.gm.RequestDocument.Request.Authentication authentication = null;
com.cultagent4.travel_republic.gm.RequestDocument.Request.Establishment establishment …Run Code Online (Sandbox Code Playgroud) 我需要使用JAXB Marshaller(JAXB 2.2版)将XML java对象序列化为XML文件.现在在xml对象中,我有一个包含String值的标记,这样:
"<"tagA>
**"<"YYYYY>done"<"/YYYYY>**
"<"/tagA>
Run Code Online (Sandbox Code Playgroud)
现在您可以看到此字符串值再次包含标记.我希望在xml文件中以相同的方式编写它.
但是JAXB Marshaller会转换这些值,例如:
"&"lt;YYYYY"&"gt;"&"#xD;done ......等等
我无法使用JAXB 2.2单独处理这些转义字符.反正可能吗?
在这方面的任何帮助都会很棒..
在此先感谢Abhinav Mishra
我使用jaxb2marshaller将少量元素用CDATA封送到XML时遇到了大麻烦.我已经完成了以下解决方案:
还有更多,但找不到合适的解决方案.他们要么告诉切换到旧的JAXB实现,要么使用MOXY.但是,这不是我的要求.我使用OXM库在下面实现了两个类,并希望生成一个XML,其中很少有元素需要附加CDATA.
import java.util.HashMap;
import java.util.Map;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
@Configuration
public class AppConfig {
@Bean
public Processor getHandler(){
Processor handler= new Processor();
handler.setMarshaller(getCastorMarshaller());
handler.setUnmarshaller(getCastorMarshaller());
return handler;
}
@Bean
public Jaxb2Marshaller getCastorMarshaller() {
Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
jaxb2Marshaller.setPackagesToScan("com.pom.dom.whatever.model");
Map<String,Object> map = new HashMap<String,Object>();
map.put("jaxb.formatted.output", true);
jaxb2Marshaller.setMarshallerProperties(map);
return jaxb2Marshaller;
}
}
Run Code Online (Sandbox Code Playgroud)
和
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
public class Processor {
private Marshaller marshaller;
private Unmarshaller unmarshalling; …Run Code Online (Sandbox Code Playgroud) 我正在使用StAX创建一个非常大的xml文档.到目前为止,我使用IndentingXMLStreamwriter类来获取格式良好的文档(另请参阅此答案).几天前,我们设置了一个jenkins服务器,它有一个较旧的jdk版本(6.26),我得到了构建错误.
package com.sun.xml.internal.txw2.output does not exist
Run Code Online (Sandbox Code Playgroud)
我认为由于安装了jdk版本,无法找到包.由于不同的原因,这不能改变(顺便说一下,有没有人知道jdk版本,这个包(com.sun.xml.internal.txw2.output)被添加了?).
因此,我正在寻找缩进的替代方案.我更喜欢类似于我使用的解决方案,这意味着无需重新分析文档.任何想法或建议?
谢谢
Lars
考虑这个例子 -
我有一个名为Report的类,它有一个Message类型的字段.Message类有一个名为"body"的字段,它是一个字符串."body"可以是任何字符串,但有时它包含格式正确的XML内容.我怎样才能确保当"body"包含XML内容时,序列化采用XML结构的形式而不是目前提供的形式?
这是输出的代码 -
报告类
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name = "Report")
@XmlType(propOrder = { "message"})
public class Report
{
private Message message;
public Message getMessage() { return message; }
public void setMessage(Message m) { message = m; }
}
Run Code Online (Sandbox Code Playgroud)
消息类
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlType(propOrder = { "body" })
public class Message
{
private String body;
public String getBody() { return body; }
@XmlElement
public void setBody(String body) { this.body = body; }
} …Run Code Online (Sandbox Code Playgroud) java ×7
jaxb ×5
xml ×3
marshalling ×2
annotations ×1
cdata ×1
character ×1
escaping ×1
indentation ×1
jaxb2 ×1
spring ×1
spring-boot ×1
stax ×1