How to Use Forbidden Characters in XML Files in Java (Avoiding Common Errors)

The XML format is widely used for data transfer between databases or in client-server communications due to its structured, platform-independent design, making it a preferred choice for handling complex data exchange.

Java can convert object lists to XML format for sending to stored procedures in XMLTYPE, and then the Oracle database can convert XMLTYPE to a cursor.

The other option, e.g., a JSON or CSV file, is a concept similar to transferring or a protocol for exchanging data, but with different data format rules.

XML, JSON, or CSV have advantages and disadvantages, and it is up to the Developer to choose which one is suitable for the project.

Example 1: Convert an XML file to a Java object.

  1. Create the ConvertXmlToObject Class.
public void ConvertXmlToObject(){
    try {
        // Create Jackson XML mapper
        XmlMapper xmlMapper = new XmlMapper();

        // Specify the path to your XML file
        File xmlFile = new File("demo.xml");

        // Read XML and convert to Java object
        Persons example = xmlMapper.readValue(xmlFile, Persons.class);

        // Access the data
        System.out.println(example.toString());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

2. Create an ArrayList of Person Java beans.

package com.entity;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public final class Persons {
   @JacksonXmlElementWrapper(useWrapping = false)
    private List<Person> Person;

}

3. Create a Person Java bean.

package com.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class Person {

    private String firstName;
    private String lastName;
    private int sex;
    private int age;

}

4. Create an XML file.

<?xml version="1.0" encoding="UTF-8"?>
<Persons>
    <Person>
        <firstName>Jane</firstName>
        <lastName>Doe</lastName>
        <sex>2</sex>
        <age>30</age>
    </Person>
    <Person>
        <firstName>John</firstName>
        <lastName>Doe</lastName>
        <sex>1</sex>
        <age>45</age>
    </Person>
</Persons>

5. Output the result without any error.

Persons(Person=[Person(firstName=Jane, lastName=Doe, sex=2, age=30), Person(firstName=John, lastName=Doe, sex=1, age=45)])

But, if an XML file is in an incorrect data format, the application causes an exception error: Expected “;” or “com.fasterxml.jackson.databind.JsonMappingException: Unexpected character ‘<’ (code 60); expected a semi-colon after the reference for entity”.

Example 2: Change data in an XML file.

  1. Create an XML file with forbidden XML characters.
<?xml version="1.0" encoding="UTF-8"?>
<Persons>
    <Person>
        <firstName>Ja&ne</firstName>
        <lastName>Doe</lastName>
        <sex>2</sex>
        <age>30</age>
    </Person>
    <Person>
        <firstName>John</firstName>
        <lastName>Doe</lastName>
        <sex>1</sex>
        <age>45</age>
    </Person>
</Persons>

2. Output an error message for forbidden XML characters.

com.fasterxml.jackson.databind.JsonMappingException: Unexpected character '<' (code 60); expected a semi-colon after the reference for entity 'ne'
unexpected character encountered while parsing value

In XML files, having an Ampersand(“&”) is one of the forbidden characters, which causes an error.

Handling Forbidden XML Characters with Java

The solution is if the developer wants to use forbidden characters in XML.

The developer must change forbidden characters according to the rules of the XML data format.

  1. < (Less Than) change to &lt;.
  2. > (Greater Than) change to &gt;.
  3. & (Ampersand) change to &amp;.
  4. “ (Double Quote) change to &quot;.
  5. ‘ (Single Quote/Apostrophe) change to &apos;.

Example 3: Forbidden characters transform according to the rules of the data format of XML.

  1. Create an XML file with forbidden XML characters following the rule.
<?xml version="1.0" encoding="UTF-8"?>
<Persons>
    <Person>
        <firstName>Jane&lt;&apos;&quot;&amp;&gt;</firstName>
        <lastName>Doe</lastName>
        <sex>2</sex>
        <age>30</age>
    </Person>
    <Person>
        <firstName>John</firstName>
        <lastName>Doe</lastName>
        <sex>1</sex>
        <age>45</age>
    </Person>
</Persons>

2. Output the result without any error.

Persons(Person=[Person(firstName=Jane<'"&>, lastName=Doe, sex=2, age=30), Person(firstName=John, lastName=Doe, sex=1, age=45)])

Java can convert XML files to Java objects correctly and without any errors.

Handling Forbidden XML Characters with Apache Commons

Use StringEscapeUtils from Apache Commons Lang to escape forbidden characters:

String escapedXml = StringEscapeUtils.escapeXml10(input);

Handling Forbidden XML Characters with JAXB for Serialization.

// Create JAXB context for the Person class
JAXBContext context = JAXBContext.newInstance(Person.class);

// Create Unmarshaller
Unmarshaller unmarshaller = context.createUnmarshaller();
// Unmarshal XML file to Java object
File xmlFile = new File("person.xml");
Person person = (Person) unmarshaller.unmarshal(xmlFile);

Finally

This solution will help the developer fix an error.