42 lines
1.2 KiB
Java
42 lines
1.2 KiB
Java
package com.dbgen;
|
|
|
|
import org.w3c.dom.Document;
|
|
import org.w3c.dom.Element;
|
|
import org.w3c.dom.Node;
|
|
import org.w3c.dom.NodeList;
|
|
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
import java.io.File;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public final class XMLUtils {
|
|
|
|
public XMLUtils() {
|
|
}
|
|
|
|
public static List<Element> getChildElements(Element e) {
|
|
List<Element> list = new ArrayList<>();
|
|
NodeList nodes = e.getChildNodes();
|
|
for (int i = 0; i < nodes.getLength(); i++) {
|
|
Node node = nodes.item(i);
|
|
if (node.getNodeType() == Node.ELEMENT_NODE)
|
|
list.add((Element) node);
|
|
}
|
|
return list;
|
|
}
|
|
private static DocumentBuilder createDocumentBuilder() throws ParserConfigurationException {
|
|
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
factory.setXIncludeAware(true);
|
|
factory.setNamespaceAware(true);
|
|
return factory.newDocumentBuilder();
|
|
}
|
|
|
|
public static Element getRootElement(File file) throws Exception {
|
|
DocumentBuilder db = createDocumentBuilder();
|
|
final Document doc = db.parse(file);
|
|
return doc.getDocumentElement();
|
|
}
|
|
} |