diff --git a/commons-serialize-xml/src/main/java/io/gitlab/jfronny/commons/serialize/xml/NativeXmlReader.java b/commons-serialize-xml/src/main/java/io/gitlab/jfronny/commons/serialize/xml/NativeXmlReader.java index 3f4c6e9..80a3ad3 100644 --- a/commons-serialize-xml/src/main/java/io/gitlab/jfronny/commons/serialize/xml/NativeXmlReader.java +++ b/commons-serialize-xml/src/main/java/io/gitlab/jfronny/commons/serialize/xml/NativeXmlReader.java @@ -822,4 +822,33 @@ public class NativeXmlReader implements Closeable { stackSize = 1; in.close(); } + + /** + * Copies the current element to the writer. + * + * @param writer the writer to copy to + * @throws IOException if an error occurs + */ + public void copyTo(NativeXmlWriter writer) throws IOException { + switch (peek()) { + case BEGIN_TAG -> { + beginTag(); + writer.beginTag(pathNames[stackSize - 1]); + while (hasNext()) { + copyTo(writer); + } + endTag(); + writer.endTag(); + } + case END_TAG -> throw new IllegalStateException("Cannot copy standalone END_TAG"); + case TEXT -> writer.text(nextText()); + case CDATA -> writer.cdata(nextCData()); + case ATTRIBUTE_NAME -> { + String name = nextAttributeName(); + writer.attribute(name, nextAttributeValue()); + } + case ATTRIBUTE_VALUE -> writer.attributeValue(nextAttributeValue()); + case EOF -> throw new IllegalStateException("Cannot copy END_DOCUMENT"); + } + } }