/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
{
t.start();
}
static class ParsingExample
implements Runnable { public void run(){
try{
final List<ElementContainer> elements = new ArrayList<>();
final DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final Document document
= documentBuilder.
parse(is
);
processNodes(document.getChildNodes(), null, elements);
for(ElementContainer ec : elements){
}
}
}
private void processNodes(final NodeList list, final ElementContainer parent, final List<ElementContainer> elements){
for(int i=0; i< list.getLength(); i++){
final Node node = list.item(i);
if("#text".equals(node.getNodeName()) || "#cdata-section".equals(node.getNodeName())){
parent.setValue(node.getNodeValue());
}
else{
final ElementContainer p = processNode(node, elements);
processNodes(node.getChildNodes(), p, elements);
}
}
}
private ElementContainer processNode(Node node, List<ElementContainer> elements){
final ElementContainer element = new ElementContainer(node);
//System.out.println(element);
if(node.hasAttributes()){
final NamedNodeMap attrs = node.getAttributes();
for(int k=0; k<attrs.getLength(); k++){
final Node attr = attrs.item(k);
element.addAttribute(attr.getNodeName(), attr.getNodeValue());
}
}
elements.add(element);
return element;
}
return "<company> <staff id='1001' adress='new york'> <firstname>yong</firstname> <lastname>mook kim</lastname> <nickname>mkyong</nickname> <salary>100000</salary> </staff> </company>";
}
return " <page><columnHeader><band height='20' splitType='Stretch'><text><![CDATA[HEADER]]></text></band></columnHeader><detail><band height='20' splitType='Stretch'><textFieldExpression><![CDATA[$F{VALUES}]]></textFieldExpression></band></detail><footer><band height='20' splitType='Stretch'><text><![CDATA[FOOTER]]></text></band></footer></page>";
}
private class ElementContainer {
private final Map
<String, String
> attributes
; private final short type;
public ElementContainer(final Node node){
attributes = new HashMap<>();
this.name = node.getNodeName();
this.value = null == node.getNodeValue() ? "" : node.getNodeValue();
this.type = node.getNodeType();
}
public void addAttribute
(final String name,
final String value
){ attributes.put(name, value);
}
public void setValue
(String value
){ this.value = value;
}
return value;
}
final StringBuilder sb = new StringBuilder();
sb
.append("Type: ")
.append(type)
.append("\nName : ")
.append(name)
.append("\nValue : ")
.append(!value.trim().isEmpty() ? value.trim() : "null")
.append("\n");
if(attributes.isEmpty()){
sb.append("attributes : {null}");
}
else{
for(Map.
Entry<String, String
> entry
: attributes.
entrySet()){ sb.append("attribute name : ")
.append(entry.getKey())
.append("\nattribute value : ")
.append(entry.getValue())
.append("\n");
}
}
sb.append("\n");
return sb.toString();
}
}
}
}