import java.io.File ;
import javax.xml.parsers.* ;
import org.w3c.dom.* ;
import javax.xml.transform.* ;
import javax.xml.transform.dom.* ;
import javax.xml.transform.stream.* ;
class MainClass {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance ( ) ; //создали фабрику строителей, сложный и грамосткий процесс (по реже выполняйте это действие)
DocumentBuilder db = dbf.newDocumentBuilder ( ) ; // создали конкретного строителя документа
Node x = doc;
visit( doc, 0 ) ;
// print
/*
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(System.out);//Может быть любой поток вывода (файл, сокет ....)
TransformerFactory transFactory = TransformerFactory.newInstance(); // Об этом подробней в 4 вопросе
Transformer transformer = transFactory.newTransformer();
transformer.transform(source, result);
*/
}
public static void visit( Node node, int level) {
NodeList list = node.getChildNodes ( ) ;
for ( int i = 0 ; i < list.getLength ( ) ; i++ ) {
Node childNode = list.item ( i) ; // текущий нод
process( childNode, level + 1 ) ; // обработка
visit( childNode, level + 1 ) ; // рекурсия
}
}
public static void process( Node node, int level) {
for ( int i = 0 ; i < level; i++ ) {
}
System .
out .
print ( node.
getNodeName ( ) ) ; // работаем как с элементом (у него есть атрибуты и схема)
NamedNodeMap atts = e.getAttributes ( ) ;
try {
System .
out .
print ( ": " + atts.
getNamedItem ( "id" ) .
getNodeValue ( ) ) ; }
}
}
aW1wb3J0IGphdmEuaW8uRmlsZTsgCmltcG9ydCBqYXZheC54bWwucGFyc2Vycy4qOyAKaW1wb3J0IG9yZy53M2MuZG9tLio7IAppbXBvcnQgamF2YXgueG1sLnRyYW5zZm9ybS4qOwppbXBvcnQgamF2YXgueG1sLnRyYW5zZm9ybS5kb20uKjsKaW1wb3J0IGphdmF4LnhtbC50cmFuc2Zvcm0uc3RyZWFtLio7CiBjbGFzcyBNYWluQ2xhc3MgeyAKICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB0aHJvd3MgRXhjZXB0aW9uIHsgCiAgICBEb2N1bWVudEJ1aWxkZ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tGA0LjQsdGD0YLRiyDQuCDRgdGF0LXQvNCwKQogICAgICAgIE5hbWVkTm9kZU1hcCBhdHRzID0gZS5nZXRBdHRyaWJ1dGVzKCk7CiAgICAgICAgdHJ5IHsKICAgICAgICAJU3lzdGVtLm91dC5wcmludCgiOiAiICsgYXR0cy5nZXROYW1lZEl0ZW0oImlkIikuZ2V0Tm9kZVZhbHVlKCkpOwogICAgICAgIH0gY2F0Y2ggKEV4Y2VwdGlvbiBleCkge1N5c3RlbS5vdXQucHJpbnQoIjogLSIpO30KICAgIH0gCiAgICBTeXN0ZW0ub3V0LnByaW50bG4oKTsgCiAgfQp9IAo=
stdin
Jmx0O3Blb3BsZV9saXN0Jmd0OyAKICAmbHQ7cGVyc29uIGlkPSZxdW90OzEyMyZxdW90OyZndDsgCiAgICAmbHQ7bmFtZSZndDtNYXJsYSBCbG9nZ3MmbHQ7L25hbWUmZ3Q7IAogICAgJmx0O2JpcnRoZGF0ZSZndDsyMDEzLTAxLTIyJmx0Oy9iaXJ0aGRhdGUmZ3Q7IAogICAgJmx0O2dlbmRlciZndDtGZW1hbGUmbHQ7L2dlbmRlciZndDsgCiAgJmx0Oy9wZXJzb24mZ3Q7IAombHQ7L3Blb3BsZV9saXN0Jmd0OyA=
<people_list>
<person id="123">
<name>Marla Bloggs</name>
<birthdate>2013-01-22</birthdate>
<gender>Female</gender>
</person>
</people_list>