title: Jackson/Woodstox XML Encoded Character Interpretation
body: <p>I have been handed an XML file with instruction to read, edit, and write it using Jackson and Woodstox (as per the recommendation in the documentation).  For the most part this has not been too hard; They're both pretty darn good at what it does.  At this point, though, I have run into a problem:</p>
  
  <p>My XML objects do themselves contain XML objects.  For example:</p>
  
  <pre><code>&lt;XMLObject&gt;
      &lt;OuterObject attributeOne="1" attributeTwo="2" attributeThree="&amp;gt;"&gt;
          &lt;InnerObject&gt;&amp;lt;NestedObject&amp;gt;Blah&amp;lt;/NestedObject&amp;gt;&lt;/InnerObject&gt;
      &lt;/OuterObject&gt;
      &lt;OuterObject attributeOne="11" attributeTwo="22" attributeThree="&amp;lt;"&gt;
          &lt;InnerObject&gt;&amp;lt;NestedObject&amp;gt;Blah&amp;lt;/NestedObject&amp;gt;&lt;/InnerObject&gt;
      &lt;/OuterObject&gt;
      &lt;OuterObject attributeOne="111" attributeTwo="222" attributeThree="3" /&gt;
  &lt;XMLObject&gt;
  </code></pre>
  
  <p>The moment that I read the XML file into my Jackson-annotated Java object, all of those instances of <code>&amp;lt;</code> and <code>&amp;gt;</code> are converted by Woodstox into <code>&lt;</code> and <code>&gt;</code>, respectively.  When I write the object back out as an XML file, <code>&lt;</code> becomes <code>&amp;lt;</code> but <code>&gt;</code> stays <code>&gt;</code></p>
  
  <pre><code>&lt;XMLObject&gt;
      &lt;OuterObject attributeOne="1" attributeTwo="2" attributeThree="&gt;"&gt;
          &lt;InnerObject&gt;&amp;lt;NestedObject&gt;Blah&amp;lt;/NestedObject&gt;&lt;/InnerObject&gt;
      &lt;/OuterObject&gt;
      &lt;OuterObject attributeOne="11" attributeTwo="22" attributeThree="&amp;lt;"&gt;
          &lt;InnerObject&gt;&amp;lt;NestedObject&gt;Blah&amp;lt;/NestedObject&gt;&lt;/InnerObject&gt;
      &lt;/OuterObject&gt;
      &lt;OuterObject attributeOne="111" attributeTwo="222" attributeThree="3" /&gt;
  &lt;XMLObject&gt;
  </code></pre>
  
  <p>The simplest version of my method that is endeavoring to read the file is as follows:</p>
  
  <pre><code>@RequestMapping("readXML")
  public @ResponseBody CustomXMLObject readXML() throws Exception {
      File inputFile = new File(FILE_PATH);
      XmlMapper mapper = new XmlMapper();
      CustomXMLObject value = mapper.readValue(inputFile, CustomXMLObject .class);
  
      return value;
  }
  </code></pre>
  
  <p>And my Jackson-annotated Java object would look something like this for the example that I gave above:</p>
  
  <pre><code>import com.fasterxml.jackson.annotation.JsonInclude;
  import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
  
  @JsonInclude(JsonInclude.Include.NON_NULL)
  public class CustomXMLObject {
      @JacksonXmlProperty(isAttribute=true)
      private long attributeOne;
      @JacksonXmlProperty(isAttribute=true)
      private String attributeTwo;
      @JacksonXmlProperty(isAttribute=true)
      private String attributeThree;
      @JacksonXmlProperty(localName = "InnerObject")
      private String innerObject;
  
  
      public long getAttributeOne() {
          return attributeOne;
      }
  
      public void setAttributeOne(long attributeOne) {
          this.attributeOne = attributeOne;
      }
  
      public String getAttributeTwo() {
          return attributeTwo;
      }
  
      public void setAttributeTwo(String attributeTwo) {
          this.attributeTwo = attributeTwo;
      }
  
      public String getAttributeThree() {
          return attributeThree;
      }
  
      public void setAttributeThree(String attributeThree) {
          this.attributeThree = attributeThree;
      }
  
      public String getInnerObject() {
          return innerObject;
      }
  
      public void setInnerObject(String innerObject) {
          this.innerObject = innerObject;
      }
  }
  </code></pre>
  
  <p>Finally, my dependencies look like this:</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
      &lt;scope&gt;test&lt;/scope&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;com.jayway.jsonpath&lt;/groupId&gt;
      &lt;artifactId&gt;json-path&lt;/artifactId&gt;
      &lt;scope&gt;test&lt;/scope&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;com.fasterxml.jackson.module&lt;/groupId&gt;
      &lt;artifactId&gt;jackson-module-jaxb-annotations&lt;/artifactId&gt;
      &lt;version&gt;2.5.0&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
          &lt;groupId&gt;com.fasterxml.jackson.dataformat&lt;/groupId&gt;
          &lt;artifactId&gt;jackson-dataformat-xml&lt;/artifactId&gt;
          &lt;version&gt;2.8.4&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.codehaus.woodstox&lt;/groupId&gt;
      &lt;artifactId&gt;woodstox-core-asl&lt;/artifactId&gt;
      &lt;version&gt;4.4.1&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>This  appears to be occurring due to Jackson's use of Woodstox' BufferingXmlWriter.  This particular writer will intercept those characters and encode them, and there does not appear to be any way to circumvent that decision:</p>
  
  <pre><code>private final void writeAttrValue(String value, int len) throws IOException {
      int inPtr = 0;
      char qchar = this.mEncQuoteChar;
      int highChar = this.mEncHighChar;
  
      while(true) {
          String ent = null;
  
          while(true) {
              if(inPtr &gt;= len) {
                  return;
              }
  
              char c = value.charAt(inPtr++);
              if(c &lt;= 60) {
                  if(c &lt; 32) {
                      if(c == 13) {
                          if(this.mEscapeCR) {
                              break;
                          }
                      } else {
                          if(c == 10 || c == 9 || this.mXml11 &amp;&amp; c != 0) {
                              break;
                          }
  
                          c = this.handleInvalidChar(c);
                      }
                  } else {
                      if(c == qchar) {
                          ent = this.mEncQuoteEntity;
                          break;
                      }
  
                      if(c == 60) {
                          ent = "&amp;lt;";
                          break;
                      }
  
                      if(c == 38) {
                          ent = "&amp;amp;";
                          break;
                      }
                  }
              } else if(c &gt;= highChar) {
                  break;
              }
  
              if(this.mOutputPtr &gt;= this.mOutputBufLen) {
                  this.flushBuffer();
              }
  
              this.mOutputBuffer[this.mOutputPtr++] = c;
          }
  
          if(ent != null) {
              this.writeRaw(ent);
          } else {
              this.writeAsEntity(value.charAt(inPtr - 1));
          }
      }
  }
  </code></pre>
  
  <p>So to sum up the problem at the end, I have been given an XML file.  That XML file contains attributes and elements that, themselves, contain symbols (<code>&lt;</code> and <code>&gt;</code>) that have been encoded (<code>&amp;lt;</code> and <code>&amp;gt;</code>) so as not to break the XML.  When Woodstox reads the file, instead of handing my Java object the actual string contained in the XML, it decodes the character.  Upon writing, only <code>&lt;</code> is re-encoded as <code>&amp;lt;</code>.  This appears to be happening because Jackson is using Woodstox' BufferingXmlWriter, which does not seem to be configurable to avoid encoding these characters.</p>
  
  <p>As a result, my question is the following:</p>
  
  <p>Can I configure the Jackson object to use a Woodstox XML reader that will allow my to read and write the characters in my XML file without further encoding, or do I need to look into a different solution entirely for my needs?</p>