Java Code Examples for org.w3c.dom.Document#hasChildNodes()

The following examples show how to use org.w3c.dom.Document#hasChildNodes() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Bats   File: DTConfiguration.java    License: Apache License 2.0 6 votes vote down vote up
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());
        }
      }
    }
  }
}
 
Example 2
Source Project: attic-apex-core   File: DTConfiguration.java    License: Apache License 2.0 6 votes vote down vote up
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());
        }
      }
    }
  }
}
 
Example 3
Source Project: oxTrust   File: BuildVersionService.java    License: MIT License 6 votes vote down vote up
@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);
	}

}
 
Example 4
Source Project: EpubParser   File: Reader.java    License: Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 5
Source Project: gama   File: DocProcessor.java    License: GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 6
Source Project: java-client-api   File: DOMWriter.java    License: Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 7
/**
 * 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);
}
 
Example 8
Source Project: EpubParser   File: Reader.java    License: Apache License 2.0 4 votes vote down vote up
private void parseOpfFile(Document document, Package pckage) throws ReadingException {
	if (document.hasChildNodes()) {
		isFoundNeeded = false;
		traverseDocumentNodesAndFillContent(document.getChildNodes(), pckage);
	}
}
 
Example 9
Source Project: EpubParser   File: Reader.java    License: Apache License 2.0 4 votes vote down vote up
private void parseTocFile(Document document, Toc toc) throws ReadingException {
	if (document.hasChildNodes()) {
		isFoundNeeded = false;
		traverseDocumentNodesAndFillContent(document.getChildNodes(), toc);
	}
}
 
Example 10
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);
}