下面列出了org.w3c.dom.Document#hasChildNodes ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void loadFile(File file, Scope defaultScope) throws IOException, ParserConfigurationException, SAXException, ConfigException
{
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
Element documentElement = doc.getDocumentElement();
if (!documentElement.getNodeName().equals("configuration")) {
throw new ConfigException("Root element needs to be \"configuration\"");
}
if (doc.hasChildNodes()) {
NodeList propertyNodes = documentElement.getChildNodes();
for (int i = 0; i < propertyNodes.getLength(); i++) {
Node propertyNode = propertyNodes.item(i);
if (propertyNode.getNodeType() == Node.ELEMENT_NODE) {
if (propertyNode.getNodeName().equals("property")) {
processPropertyNode((Element)propertyNode, defaultScope);
} else {
LOG.warn("Ignoring unknown element {}", propertyNode.getNodeName());
}
}
}
}
}
public void loadFile(File file, Scope defaultScope) throws IOException, ParserConfigurationException, SAXException, ConfigException
{
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
Element documentElement = doc.getDocumentElement();
if (!documentElement.getNodeName().equals("configuration")) {
throw new ConfigException("Root element needs to be \"configuration\"");
}
if (doc.hasChildNodes()) {
NodeList propertyNodes = documentElement.getChildNodes();
for (int i = 0; i < propertyNodes.getLength(); i++) {
Node propertyNode = propertyNodes.item(i);
if (propertyNode.getNodeType() == Node.ELEMENT_NODE) {
if (propertyNode.getNodeName().equals("property")) {
processPropertyNode((Element)propertyNode, defaultScope);
} else {
LOG.warn("Ignoring unknown element {}", propertyNode.getNodeName());
}
}
}
}
}
@PostConstruct
public void initalize() {
try (InputStream is = getClass().getResourceAsStream("/META-INF/beans.xml")) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = factory.newDocumentBuilder();
Document doc = dBuilder.parse(is);
doc.getDocumentElement().normalize();
log.info("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("bean");
if (doc.hasChildNodes()) {
readBuildDetails(nList);
}
} catch (Exception ex) {
log.error("Failed to obtain build version", ex);
}
}
private void parseContainerXml(DocumentBuilder docBuilder, Document document, ZipFile epubFile) throws ReadingException {
if (document.hasChildNodes()) {
isFoundNeeded = false;
traverseDocumentNodesAndFillContent(document.getChildNodes(), content.getContainer());
}
String opfFilePath = content.getContainer().getFullPathValue();
ZipEntry opfFileEntry = epubFile.getEntry(opfFilePath);
if (opfFileEntry == null) {
for (String entryName : content.getEntryNames()) {
if (entryName.contains(Constants.EXTENSION_OPF)) {
opfFileEntry = epubFile.getEntry(entryName);
break;
}
}
}
if (opfFileEntry == null) {
throw new ReadingException(".opf file not found");
}
InputStream opfFileInputStream;
try {
opfFileInputStream = epubFile.getInputStream(opfFileEntry);
} catch (IOException e) {
e.printStackTrace();
throw new ReadingException("IO error while reading " + Constants.EXTENSION_OPF + " inputstream: " + e.getMessage());
}
Document packageDocument = getDocument(docBuilder, opfFileInputStream, Constants.EXTENSION_OPF);
parseOpfFile(packageDocument, content.getPackage());
}
protected org.w3c.dom.Element getRootNode(final Document doc) {
org.w3c.dom.Element root = null;
if (doc.hasChildNodes()) {
root = (org.w3c.dom.Element) doc.getElementsByTagName(getRootName()).item(0);
}
if (root == null) {
root = doc.createElement(getRootName());
doc.appendChild(root);
}
return root;
}
public void serializeDocument(Document document) throws XMLStreamException {
String encoding = document.getInputEncoding();
String version = document.getXmlVersion();
if (encoding != null) {
serializer.writeStartDocument(encoding, version);
} else {
serializer.writeStartDocument(version);
}
if (document.hasChildNodes()) {
serializeNodeList(document.getChildNodes());
}
serializer.writeEndDocument();
}
/**
* Parse root node
*/
private List<String> findProfileSubsystemsInXML(Document doc, String profileName) {
List<String> output = null;
if (!doc.hasChildNodes()) {
return output;
}
NodeList nodeList = doc.getChildNodes();
return findProfileSubsystems(nodeList, profileName);
}
private void parseOpfFile(Document document, Package pckage) throws ReadingException {
if (document.hasChildNodes()) {
isFoundNeeded = false;
traverseDocumentNodesAndFillContent(document.getChildNodes(), pckage);
}
}
private void parseTocFile(Document document, Toc toc) throws ReadingException {
if (document.hasChildNodes()) {
isFoundNeeded = false;
traverseDocumentNodesAndFillContent(document.getChildNodes(), toc);
}
}
public static void createTests(final ProcessorContext context, final Document doc) {
if (doc == null || !doc.hasChildNodes()) { return; }
final Document document = cleanDocumentTest(doc);
createOperatorsTests(context, document, OPERATORS_TRANSFORMER);
}