我需要JIBX编组帮助。
我有一个字符串“这是测试&lt ;:test>这是测试”;
编组后,这将转换为
<cus:street>This is test <:test> This is test</cus:street>
请建议一个解决方案,如何防止&lt;从而无需在架构中进行任何更改即可从java代码中获取。
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:cus="http://www.trivadis.com/cdm/customer"
targetNamespace="http://www.trivadis.com/cdm/customer"
elementFormDefault="qualified">
<xsd:element name="customer" type="cus:CustomerType"/>
<xsd:complexType name="CustomerType">
<xsd:sequence>
<xsd:element name="id" type="xsd:integer"/>
<xsd:element name="firstName" type="xsd:string"/>
<xsd:element name="lastName" type="xsd:string"/>
<xsd:element name="address" type="cus:AddressType"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="AddressType">
<xsd:sequence>
<xsd:element name="street" type="xsd:string"/>
<xsd:element name="city" type="xsd:string"/>
<xsd:element name="zipCode" type="xsd:integer"/>
<xsd:element name="country" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
package com.trivadis.soa;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import junit.framework.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.trivadis.cdm.customer.Address;
import com.trivadis.cdm.customer.Customer;
@RunWith(SpringJUnit4ClassRunner.class)
//ApplicationContext will be loaded from "classpath:/com/trivadis/soa/PublisherTest- context.xml"
@ContextConfiguration
public class MarshallingTest {
@Autowired
private Marshaller marshaller;
@Autowired
private Unmarshaller unmarshaller;
private static final String FILE_NAME = "G:/spring-oxm-jibx-sample/NewFile.xml";
@Test
public void testMarshalUnmarshalCustomer() throws Exception {
Customer cust = null;
Customer custRead = null;
cust = new Customer();
cust.setId(1);
cust.setFirstName("Guido");
cust.setLastName("Schmutz");
Address adr = new Address();
adr.setStreet("This is test <test> this is test");
adr.setZipCode(3014);
adr.setCity("Bern");
adr.setCountry("Switzerland");
cust.setAddress(adr);
FileOutputStream os = null;
try {
os = new FileOutputStream(FILE_NAME);
marshaller.marshal(cust, new StreamResult(os));
} finally {
if (os != null) {
os.close();
}
}
FileInputStream is = null;
try {
is = new FileInputStream(FILE_NAME);
custRead = (Customer) this.unmarshaller.unmarshal(new StreamSource(is));
} finally {
if (is != null) {
is.close();
}
}
Assert.assertEquals(cust.getId(), custRead.getId());
Assert.assertEquals(cust.getFirstName(), custRead.getFirstName());
Assert.assertEquals(cust.getLastName(), custRead.getLastName());
//Assert.assertEquals(cust.getAddress().getStreet(), custRead.getAddress().getStreet());
Assert.assertEquals(cust.getAddress().getZipCode(), custRead.getAddress().getZipCode());
Assert.assertEquals(cust.getAddress().getCity(), custRead.getAddress().getCity());
Assert.assertEquals(cust.getAddress().getCountry(), custRead.getAddress().getCountry());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<cus:customer xmlns:cus="http://www.trivadis.com/cdm/customer">
<cus:id>1</cus:id>
<cus:firstName>Guido</cus:firstName>
<cus:lastName>Schmutz</cus:lastName>
<cus:address>
<cus:street>This is test <test> this is test</cus:street>
<cus:city>Bern</cus:city>
<cus:zipCode>3014</cus:zipCode>
<cus:country>Switzerland</cus:country>
</cus:address>
</cus:customer>