/
NikolayIvkin
/
tutorials1
Обзор
Документация
Войти
/
NikolayIvkin
/
tutorials1
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
xml-2/src/test/java/com/baeldung/xml/xml2string/XmlDocumentToStringUnitTest.java
32 строки
1 KB
Jonathan Cook
BAEL-7864 - How to convert org.w3c.dom.Document to String in Java (#16713)
24 май 2024, 19:24
Не верифицирован
24 май 2024, 19:24
9d617aa
Код
Авторство
О чём код?
package com.baeldung.xml.xml2string; import static org.junit.Assert.assertEquals; import org.junit.Test; import org.w3c.dom.Document; public class XmlDocumentToStringUnitTest { @Test public void givenXMLDocument_thenConvertToStringSuccessfully() throws Exception { Document document = XmlDocumentToString.getDocument(); String expectedDeclartion = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"; assertEquals(expectedDeclartion + XmlDocumentToString.FRUIT_XML, XmlDocumentToString.toString(document)); } @Test public void givenXMLDocument_thenConvertToStringWithOutputOptionsSuccessfully() throws Exception { Document document = XmlDocumentToString.getDocument(); String expected = "<fruit>\n" + " <name>Apple</name>\n" + " <color>Red</color>\n" + " <weight unit=\"grams\">150</weight>\n" + " <sweetness>7</sweetness>\n" + "</fruit>\n"; assertEquals(expected, XmlDocumentToString.toStringWithOptions(document)); } }