How do you display XML content with line feeds (pretty print) in a PrimeFaces dataTable column?
My XML content is already in pretty print format and I can see it with line feeds when doing a System.out.println from a FacesConverter that converts the bytes to a String before it is rendered on the table column.
Here is the code for the FacesConverter:
@FacesConverter(value="xmlStream")
public class ByteConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
byte[] buffer = null;
try{
buffer = value.getBytes("UTF-8");
}catch(UnsupportedEncodingException e){
buffer = value.getBytes();
}
return buffer;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
String text = null;
if (value instanceof byte[]){
try{
text = new String((byte[])value, "UTF-8");
}catch(UnsupportedEncodingException e){
text = new String((byte[])value);
}
}
System.out.println("text=<" + text + ">");
return text;
}
}
And here is what I see in the console when doing a System.out.println above:
<linkUp>
<ifName>eth1</ifName>
<linkProperty>
<name>1</name>
<flags>42</flags>
<extensions>
<name>foo</name>
<value>bar2</value>
</extensions>
</linkProperty>
<linkProperty>
<name>2</name>
<flags>4668</flags>
<extensions>
<name>foo</name>
<value>bar2</value>
</extensions>
</linkProperty>
</linkUp>
But when the data displays on the PF dataTable column, it appears as a single line string without line feeds - Is there a way to display it as it displays on the console? How do others display XML content in a PF table?
Hmmm, It looks like this does the trick:
.whiteSpaceClass{
white-space: pre-wrap;
}
But now I am having to deal with rows that span half the page because the XML is so large. I guess the old saying 'Beware of what you ask for' applies here...:)
Still, it would be nice to display just the first few lines and be able to expand / collapse the rest based on single click event, or even display it in a box when the cell is double clicked - problem is, I have no idea how to do that or if PF supports that...:)
You could use expandable rows to show your entire XML. See a example of use of p:rowExpansion in showcase.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With