Chỉ cần một ví dụ về Blaise Doughan câu trả lời, với ContentHandler:
import java.io.IOException;
import java.io.Writer;
import org.apache.commons.lang3.StringUtils;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class WriteOutContentHandler extends DefaultHandler
{
private static final String NEWLINE = System.getProperty("line.separator");
private static final String INDENT = " ";
private Writer _writer;
private int depth = 0;
public WriteOutContentHandler(Writer writer)
{
_writer = writer;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException
{
try
{
_writer.write(ch, start, length);
} catch (IOException e)
{
throw new SAXException("Error writing out character content", e);
}
}
@Override
public void ignorableWhitespace(char[] ch, int start, int length)
throws SAXException
{
try
{
_writer.write(ch, start, length);
} catch (IOException e)
{
throw new SAXException("Error writing out character content", e);
}
}
@Override
public void endDocument() throws SAXException
{
try
{
_writer.flush();
} catch (IOException e)
{
throw new SAXException("Error flushing character output", e);
}
}
@Override
public String toString()
{
return _writer.toString();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attrs) throws SAXException
{
write(NEWLINE);
write(StringUtils.repeat(INDENT, depth));
depth++;
String eName = localName;
if ("".equals(eName))
{
eName = qName;
}
write("<" + eName);
if (attrs != null)
{
for (int i = 0; i < attrs.getLength(); i++)
{
String attrName = attrs.getLocalName(i);
if ("".equals(attrName))
{
attrName = attrs.getQName(i);
}
write(NEWLINE);
write(StringUtils.repeat(INDENT, depth));
write(attrName);
write("=\"");
write(attrs.getValue(i));
write("\"");
}
}
if (attrs.getLength() > 0)
{
write(NEWLINE);
write(StringUtils.repeat(INDENT, depth-1));
}
write(">");
}
@Override
public void endElement(String namespaceURI, String sName, String qName) throws SAXException
{
write(NEWLINE);
depth--;
write(StringUtils.repeat(INDENT, depth));
String eName = sName;
if ("".equals(eName))
{
eName = qName;
}
write("</" + eName + ">");
}
private void write(String s) throws SAXException
{
try
{
_writer.write(s);
_writer.flush();
} catch (IOException e)
{
throw new SAXException("I/O error", e);
}
}
}
Và sử dụng:
StringWriter writer = new StringWriter();
JAXBContext jc = JAXBContext.newInstance(MODEL);
Marshaller marshaller = jc.createMarshaller();
marshaller.marshal(node, new WriteOutContentHandler(writer));
return writer.toString();
(http://stackoverflow.com/questions/1823979/how-to-have-line-breaks-in-xml-attributes?rq=1) – Damo
Đây không phải là những gì tôi đã hỏi ... Tôi cần các thuộc tính được bao bọc, chứ không phải giá trị của chúng. Nhìn vào ví dụ. –
Xin chào David Fischer, Bạn có thể giải quyết vấn đề này không? – neni