简体   繁体   中英

Java, parse raw, unconverted value of an xml attribute. Jaxson or jaxb

Given following XML input, how can I parse raw value of "descr" attribute?

  <?xml version="1.0"?>
  <declaration descr="&#xA0;"/>

I tried following with JAXB and Jackson. In both cases space is printed, while I expect

 &#xA0;

JaxB test:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "declaration")
public class DeclarationTestJaxb {

  @XmlAttribute
  private String descr;

  public String getDescr() {
      return descr;
  }

  public void setDescr(String descr) {
      this.descr = descr;
  }

  @Override
  public String toString() {
      return descr;
    }

  public static void main(String[] args) throws JAXBException {
    Unmarshaller unmarshaller =  JAXBContext.newInstance(DeclarationTestJaxb.class).createUnmarshaller();
    System.out.println(unmarshaller.unmarshal(new File("test.xml")));
  }
}     

I tried the same with @JsonRawValue and @JacksonXmlProperty(isAttribute = true). However the output is still converted.

You can't. XML parsers always decode entities before returning strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM