XStream: Store parts of XML as XML

I have the following XML:

<patient>
    <name>Mr. Sick</name>
    <report>
        <paragraph><bold>Conclusion</bold>text...</paragraph>
    </report>
</patient>

I would like to convert this to an instance of the Patient class as follows:

Class Patient {
    String name = "Mr. Sick";
    String report = "<paragraph><bold>Conclusion</bold>text...</paragraph>";
}

Can I use XStream to convert only part of XML and save the report field in XML format? How can I do that?

+5
source share
3 answers

I solved this by creating a Convertor implementation as described here . My solution to the problem is this:

Patient.java

public class Patient {
    String name;
    Report report;
}

Report.java

public class Report {
    public String report;
}

Converter implementation for XStream

public class ReportConverter implements Converter { 

    @Override 
    public boolean canConvert(Class classs) { 
        System.out.println("canConvert: " + classs.getName());
        return classs.equals(Report.class); 
    } 

    @Override 
    public void marshal(Object value, HierarchicalStreamWriter writer, 
            MarshallingContext context) {
        // not used in this example
    } 

    // goes recursive through all the nodes in <report>
    String getNodeAsText(HierarchicalStreamReader reader) {
        String result;
        result = "<" + reader.getNodeName() + ">" + reader.getValue();
        while (reader.hasMoreChildren() ) {
            reader.moveDown();
            result += getNodeAsText(reader);
            reader.moveUp();
            result += reader.getValue();
        }
        result += "</" + reader.getNodeName() + ">";
        return result;
    }

    @Override 
    public Object unmarshal(HierarchicalStreamReader reader, 
            UnmarshallingContext context) {
        Report xReport = new Report();
        xReport.report = reader.getValue();
        while (reader.hasMoreChildren() ) {
            reader.moveDown();
            xReport.report += getNodeAsText(reader);
            reader.moveUp();
            xReport.report += reader.getValue();
        }
        return xReport; 
    } 
}

Converter usage example with XStream

XStream xStream = new XStream();
xStream.registerConverter(new ReportConverter());
xStream.alias("patient", Patient.class);
xStream.alias("report", Report.class);
String xml = "<patient><name>Mr. Sick</name><report><paragraph>" +
        "some text here<bold>Conclusion</bold>text...</paragraph>" +
        "<sdf>hello world</sdf></report></patient>";
Patient patient = (Patient)xStream.fromXML(xml);
System.out.println("patient.name: " + patient.name);
System.out.println("patient.report: " + patient.report.report);

Output

patient: Mr. Sick
patient.report: <paragraph>some text here<bold>Conclusion</bold>text...
    ...</paragraph><sdf>hello world</sdf>
+4
source

I have no answer to the question asked, but I offer an alternative that can help.

, report. xml, .

( commons libray apache org.apache.commons.lang.StringEscapeUtils):

class Patient {
   String name = "Mr. Sick";
   String report = StringEscapeUtils.escapeHtml(
         "<paragraph><bold>Conclusion</bold>text...</paragraph>");

  // report = "&lt;paragraph&gt;&lt;bold&gt;Conclusion&lt;/bold&gt;text...&lt;/paragraph&gt;"
}
+3

The best version of getNodeAsText (), which includes the attributes of each node:

private String getNodeAsText(HierarchicalStreamReader reader) {

StringBuilder str = new StringBuilder();
str.append("<")
   .append(reader.getNodeName());

for (int i = 0; i < reader.getAttributeCount(); i++) {
  String name = reader.getAttributeName(i);
  String value = reader.getAttribute(i);
  str.append(" ")
     .append(name)
     .append("=\"")
     .append(value)
     .append("\"");
}
str.append(">")
   .append(reader.getValue());

while (reader.hasMoreChildren()) {
  reader.moveDown();
  str.append(getNodeAsText(reader));
  reader.moveUp();
  str.append(reader.getValue());
}
str.append("</")
   .append(reader.getNodeName())
   .append(">");

return str.toString();

}

code>

+2
source

All Articles