Naveen Kumar Mishra
Naveen Kumar Mishra

Reputation: 97

How to remove extra escape character while doing marshling using jaxb

Original XML amp; is added by JAXB which need to be ignore :-

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <emp>
 <address>7 stret &amp; new </address>
 <name>Naveenqq</name>
</emp>

expected without amp;(actual value want) :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <emp>
  <address>7 stret & new </address>
  <name>Naveenqq</name>
</emp>

I have tried below code:

  private static void jaxbObjectToXML(Emp employee) throws IOException, SAXException, ParserConfigurationException 
{
    try
    { 

        JAXBContext jaxbContext = JAXBContext.newInstance(Emp.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        //jaxbMarshaller.setProperty("jaxb.encoding", "US-ASCII"); 
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
        //jaxbMarshaller.setProperty(OutputKeys.ENCODING, "ASCII");
        //jaxbMarshaller.setProperty(CharacterEscapeHandler.class.getName(), new CustomCharacterEscapeHandler());
        //          jaxbMarshaller.setProperty(CharacterEscapeHandler.class.getName(), new CharacterEscapeHandler() {
        //            
        //              @Override
        //              public void escape(char[] ch, int start, int length, boolean isAttVal, Writer out) throws IOException {
        //                  out.write( ch, start, length ); 
        //                  
        //              }
        //          }); 
        //          
        //          StringWriter writer = new StringWriter();
        File file = new File("employee1.xml");
        jaxbMarshaller.marshal(employee, file); 
        //          
        //          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        //          DocumentBuilder builder = factory.newDocumentBuilder();
        //          InputSource is = new InputSource( new StringReader( writer.toString() ) );
        //          Document doc = builder.parse( is );
        System.out.println("done::");


    } 
    catch (JAXBException e) 
    {
        e.printStackTrace();
    }
}

Please help how to resolve same , I have tried all encoding type

Upvotes: 0

Views: 630

Answers (2)

BATMAN_2008
BATMAN_2008

Reputation: 3540

The problem is that & in the XML is not valid and if you try to validate an XML with & it would fail. JAXB is pretty intelligent so it tries to replace the special characters with their character entities. A similar thing happens in HTML as well. You can refer here.

But if you observe the values after JAXB Unmarshalling it has been replaced by & instead of &amp;. So you do not have to worry about it having in the XML. I guess if you take the route which you want then it would lead to many complications and your XML itself will be invalid.

XML:

<emp>
   <address>7 stret &amp; new</address>
   <name>Naveenqq</name>
</emp>

Root:

@Data
@XmlRootElement(name = "emp")
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
    private String address;
    private String name;
}

Main:

public class Main {
    public static void main(String[] args) throws JAXBException, XMLStreamException {
        final InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("test.xml");
        final XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
        final Unmarshaller unmarshaller = JAXBContext.newInstance(Root.class).createUnmarshaller();
        final Root root = unmarshaller.unmarshal(xmlStreamReader, Root.class).getValue();
        System.out.println(root.toString());

        Marshaller marshaller = JAXBContext.newInstance(Root.class).createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "US-ASCII");
        //marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", new XmlCharacterHandler());
        marshaller.marshal(root, System.out);
    }
}

Output:

Root(address=7 stret & new, name=Naveenqq)
<?xml version="1.0" encoding="US-ASCII"?>
<emp>
   <address>7 stret &amp; new</address>
   <name>Naveenqq</name>
</emp>

As you can see in the output Root(address=7 stret & new, name=Naveenqq) it has been replaced by & so you can continue to use the same.

Hope the explanation helps.

Upvotes: 2

Michael Kay
Michael Kay

Reputation: 163615

Your expected value is not valid XML, so there's no way you can persuade any XML-aware tool to generate it.

Why are you trying to generate invalid XML?

Upvotes: 1

Related Questions