Returns a string representation as XML document from this element and all it's children (recursively). The charset used in the xml header is the current page encoding; but the result is still a string. You have to make sure to use the correct (in fact the same) encoding if you write this to a fi
()
| 782 | * @return the XML string |
| 783 | */ |
| 784 | public String asXml() { |
| 785 | Charset charsetName = null; |
| 786 | final HtmlPage htmlPage = getHtmlPageOrNull(); |
| 787 | if (htmlPage != null) { |
| 788 | charsetName = htmlPage.getCharset(); |
| 789 | } |
| 790 | |
| 791 | final StringWriter stringWriter = new StringWriter(); |
| 792 | try (PrintWriter printWriter = new PrintWriter(stringWriter)) { |
| 793 | boolean tag = false; |
| 794 | if (charsetName != null && this instanceof HtmlHtml) { |
| 795 | printWriter.print("<?xml version=\"1.0\" encoding=\""); |
| 796 | printWriter.print(charsetName); |
| 797 | printWriter.print("\"?>"); |
| 798 | tag = true; |
| 799 | } |
| 800 | printXml("", tag, printWriter); |
| 801 | return stringWriter.toString(); |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | /** |
| 806 | * Recursively writes the XML data for the node tree starting at <code>node</code>. |