use of javax.xml.parsers.DocumentBuilderFactory in project generator by mybatis.
the class XmlFileMergerJaxp method getMergedSource.
public static String getMergedSource(InputSource newFile, InputSource existingFile, String existingFileName) throws IOException, SAXException, ParserConfigurationException, ShellException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setExpandEntityReferences(false);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver(new NullEntityResolver());
Document existingDocument = builder.parse(existingFile);
Document newDocument = builder.parse(newFile);
DocumentType newDocType = newDocument.getDoctype();
DocumentType existingDocType = existingDocument.getDoctype();
if (!newDocType.getName().equals(existingDocType.getName())) {
throw new ShellException(getString(//$NON-NLS-1$
"Warning.12", existingFileName));
}
Element existingRootElement = existingDocument.getDocumentElement();
Element newRootElement = newDocument.getDocumentElement();
// reconcile the root element attributes -
// take all attributes from the new element and add to the existing
// element
// remove all attributes from the existing root element
NamedNodeMap attributes = existingRootElement.getAttributes();
int attributeCount = attributes.getLength();
for (int i = attributeCount - 1; i >= 0; i--) {
Node node = attributes.item(i);
existingRootElement.removeAttribute(node.getNodeName());
}
// add attributes from the new root node to the old root node
attributes = newRootElement.getAttributes();
attributeCount = attributes.getLength();
for (int i = 0; i < attributeCount; i++) {
Node node = attributes.item(i);
existingRootElement.setAttribute(node.getNodeName(), node.getNodeValue());
}
// remove the old generated elements and any
// white space before the old nodes
List<Node> nodesToDelete = new ArrayList<Node>();
NodeList children = existingRootElement.getChildNodes();
int length = children.getLength();
for (int i = 0; i < length; i++) {
Node node = children.item(i);
if (isGeneratedNode(node)) {
nodesToDelete.add(node);
} else if (isWhiteSpace(node) && isGeneratedNode(children.item(i + 1))) {
nodesToDelete.add(node);
}
}
for (Node node : nodesToDelete) {
existingRootElement.removeChild(node);
}
// add the new generated elements
children = newRootElement.getChildNodes();
length = children.getLength();
Node firstChild = existingRootElement.getFirstChild();
for (int i = 0; i < length; i++) {
Node node = children.item(i);
// don't add the last node if it is only white space
if (i == length - 1 && isWhiteSpace(node)) {
break;
}
Node newNode = existingDocument.importNode(node, true);
if (firstChild == null) {
existingRootElement.appendChild(newNode);
} else {
existingRootElement.insertBefore(newNode, firstChild);
}
}
// pretty print the result
return prettyPrint(existingDocument);
}
use of javax.xml.parsers.DocumentBuilderFactory in project openhab1-addons by openhab.
the class NotifyMyAndroid method parseResponse.
private static String parseResponse(String response) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
InputSource inStream = new InputSource();
inStream.setCharacterStream(new StringReader(response));
Document doc = db.parse(inStream);
Element root = doc.getDocumentElement();
String lastError = null;
if (root.getTagName().equals("nma")) {
Node item = root.getFirstChild();
String childName = item.getNodeName();
if (!childName.equals("success")) {
lastError = item.getFirstChild().getNodeValue();
}
}
return lastError;
}
use of javax.xml.parsers.DocumentBuilderFactory in project openhab1-addons by openhab.
the class FrontierSiliconRadioApiResult method getXmlDocFromString.
/**
* converts the string we got from the radio to a parsable XML document
*
* @param xmlString
* the XML string read from the radio
* @return the parsed XML document
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
*/
private Document getXmlDocFromString(String xmlString) throws ParserConfigurationException, SAXException, IOException {
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
final DocumentBuilder builder = factory.newDocumentBuilder();
final Document xmlDocument = builder.parse(new InputSource(new StringReader(xmlString)));
return xmlDocument;
}
use of javax.xml.parsers.DocumentBuilderFactory in project openhab1-addons by openhab.
the class XMLUtil method loadXMLFromString.
/**
* Loads a string into a xml document object.
*
* @param xml
* @return
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
*/
public static Document loadXMLFromString(String xml) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
Document doc = builder.parse(is);
return doc;
}
use of javax.xml.parsers.DocumentBuilderFactory in project openhab1-addons by openhab.
the class OWServerBinding method getVariable.
String getVariable(String response, String romId, String name) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// Get the DOM Builder
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
logger.error("Error parsing OWServer XML response " + e.getMessage());
}
// Load and Parse the XML document
// document contains the complete XML as a Tree.
Document document = null;
try {
InputSource is = new InputSource(new StringReader(response));
document = builder.parse(is);
} catch (SAXException e) {
logger.error("Error reading OWServer XML response " + e.getMessage());
} catch (IOException e) {
logger.error("Error reading OWServer XML response " + e.getMessage());
}
// Iterating through the nodes and extracting the data.
NodeList nodeList = document.getDocumentElement().getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeName().startsWith("owd_")) {
boolean romMatch = false;
NodeList childNodes = node.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
Node cNode = childNodes.item(j);
// Identifying the child tag of employee encountered.
if (cNode instanceof Element) {
String content = cNode.getLastChild().getTextContent().trim();
if (cNode.getNodeName().equals("ROMId") & content.equals(romId)) {
romMatch = true;
}
String nname = cNode.getNodeName();
if (nname.equals(name) & romMatch == true) {
return content;
}
}
}
}
}
return null;
}
Aggregations