use of javax.xml.parsers.DocumentBuilder in project tinker by Tencent.
the class JavaXmlUtil method getDocumentBuilder.
/**
* get document builder
*
* @return DocumentBuilder
*/
private static DocumentBuilder getDocumentBuilder() {
DocumentBuilder documentBuilder = null;
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
try {
documentBuilder = documentBuilderFactory.newDocumentBuilder();
} catch (Exception e) {
throw new JavaXmlUtilException(e);
}
return documentBuilder;
}
use of javax.xml.parsers.DocumentBuilder in project malmo by Microsoft.
the class SchemaHelper method getRootNodeName.
/** Retrieve the name of the root node in an XML string.
* @param xml The XML string to parse.
* @return The name of the root node, or null if parsing failed.
*/
public static String getRootNodeName(String xml) {
String rootNodeName = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setExpandEntityReferences(false);
DocumentBuilder dBuilder = dbf.newDocumentBuilder();
InputSource inputSource = new InputSource(new StringReader(xml));
Document doc = dBuilder.parse(inputSource);
doc.getDocumentElement().normalize();
rootNodeName = doc.getDocumentElement().getNodeName();
} catch (SAXException e) {
System.out.println("SAX exception: " + e);
} catch (IOException e) {
System.out.println("IO exception: " + e);
} catch (ParserConfigurationException e) {
System.out.println("ParserConfiguration exception: " + e);
}
return rootNodeName;
}
use of javax.xml.parsers.DocumentBuilder in project Mycat-Server by MyCATApache.
the class FirewallConfig method updateToFile.
public static synchronized void updateToFile(String host, List<UserConfig> userConfigs) throws Exception {
LOGGER.debug("set white host:" + host + "user:" + userConfigs);
String filename = SystemConfig.getHomePath() + File.separator + "conf" + File.separator + "server.xml";
//String filename = "E:\\MyProject\\Mycat-Server\\src\\main\\resources\\server.xml";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false);
factory.setValidating(false);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver(new IgnoreDTDEntityResolver());
Document xmldoc = builder.parse(filename);
Element whitehost = (Element) xmldoc.getElementsByTagName("whitehost").item(0);
Element firewall = (Element) xmldoc.getElementsByTagName("firewall").item(0);
if (firewall == null) {
firewall = xmldoc.createElement("firewall");
Element root = xmldoc.getDocumentElement();
root.appendChild(firewall);
if (whitehost == null) {
whitehost = xmldoc.createElement("whitehost");
firewall.appendChild(whitehost);
}
}
for (UserConfig userConfig : userConfigs) {
String user = userConfig.getName();
Element hostEle = xmldoc.createElement("host");
hostEle.setAttribute("host", host);
hostEle.setAttribute("user", user);
whitehost.appendChild(hostEle);
}
TransformerFactory factory2 = TransformerFactory.newInstance();
Transformer former = factory2.newTransformer();
String systemId = xmldoc.getDoctype().getSystemId();
if (systemId != null) {
former.setOutputProperty(javax.xml.transform.OutputKeys.DOCTYPE_SYSTEM, systemId);
}
former.transform(new DOMSource(xmldoc), new StreamResult(new File(filename)));
}
use of javax.xml.parsers.DocumentBuilder in project OpenAttestation by OpenAttestation.
the class ReadXmlTest method getTagSelectionFromXml.
public TagSelection getTagSelectionFromXml(String xml) throws ParserConfigurationException, SAXException, IOException {
TagSelection ret = new TagSelection();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
Document doc = builder.parse(is);
ArrayList<tag> tagList = new ArrayList<tag>();
int cnt = 0;
NodeList nodeList = doc.getElementsByTagName("attribute");
for (int s = 0; s < nodeList.getLength(); s++) {
Node fstNode = nodeList.item(s);
if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
Element fstElmnt = (Element) fstNode;
String idValue = fstElmnt.getAttribute("oid");
Element lstNmElmnt = (Element) nodeList.item(cnt++);
NodeList lstNm = lstNmElmnt.getChildNodes();
String currentAction = ((Node) lstNm.item(0)).getNodeValue();
if (currentAction != null) {
tagList.add(new tag("", idValue, currentAction));
}
}
}
nodeList = doc.getElementsByTagName("selection");
Node fstNode = nodeList.item(0);
Element e = (Element) fstNode;
ret.id = e.getAttribute("id");
ret.name = e.getAttribute("name");
ret.tagList = tagList;
return ret;
}
use of javax.xml.parsers.DocumentBuilder in project OpenGrok by OpenGrok.
the class SubversionRepository method setDirectoryName.
@Override
public void setDirectoryName(String directoryName) {
super.setDirectoryName(directoryName);
if (isWorking()) {
// set to true if we manage to find the root directory
Boolean rootFound = Boolean.FALSE;
List<String> cmd = new ArrayList<>();
cmd.add(RepoCommand);
cmd.add("info");
cmd.add("--xml");
File directory = new File(getDirectoryName());
Executor executor = new Executor(cmd, directory);
if (executor.exec() == 0) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(executor.getOutputStream());
String url = getValue(document.getElementsByTagName("url").item(0));
if (url == null) {
LOGGER.log(Level.WARNING, "svn info did not contain an URL for [{0}]. Assuming remote repository.", directoryName);
setRemote(true);
} else {
if (!url.startsWith("file")) {
setRemote(true);
}
}
String root = getValue(document.getElementsByTagName("root").item(0));
if (url != null && root != null) {
reposPath = url.substring(root.length());
rootFound = Boolean.TRUE;
}
} catch (SAXException saxe) {
LOGGER.log(Level.WARNING, "Parser error parsing svn output", saxe);
} catch (ParserConfigurationException pce) {
LOGGER.log(Level.WARNING, "Parser configuration error parsing svn output", pce);
} catch (IOException ioe) {
LOGGER.log(Level.WARNING, "IOException reading from svn process", ioe);
}
} else {
LOGGER.log(Level.WARNING, "Failed to execute svn info for [{0}]. Repository disabled.", directoryName);
}
setWorking(rootFound);
}
}
Aggregations