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 ( ) ) ; if ( node.getNodeType ( ) == Node.ELEMENT_NODE ) {
// работаем как с элементом (у него есть атрибуты и схема)
NamedNodeMap atts = e.getAttributes ( ) ;
try {
System .
out .
print ( ": " + atts.
getNamedItem ( "id" ) .
getNodeValue ( ) ) ; }
if ( node.getNodeType ( ) == Node.TEXT_NODE ) {
System .
out .
print ( "\t " + node.
getNodeValue ( ) .
trim ( ) ) ; }
}
}
aW1wb3J0IGphdmEuaW8uRmlsZTsgCmltcG9ydCBqYXZheC54bWwucGFyc2Vycy4qOyAKaW1wb3J0IG9yZy53M2MuZG9tLio7IAppbXBvcnQgamF2YXgueG1sLnRyYW5zZm9ybS4qOwppbXBvcnQgamF2YXgueG1sLnRyYW5zZm9ybS5kb20uKjsKaW1wb3J0IGphdmF4LnhtbC50cmFuc2Zvcm0uc3RyZWFtLio7CiBjbGFzcyBNYWluQ2xhc3Mgey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expression>
<operation id="+">
<variable>x</variable>
<number>10</number>
<operation id="*">
<number>2</number>
<number>3</number>
</operation>
<number>12</number>
</operation>
</expression>