use of javax.xml.parsers.DocumentBuilderFactory in project qi4j-sdk by Qi4j.
the class RssReaderTest method testReadRssFeed.
@Test
public void testReadRssFeed() {
Client client = new Client(Protocol.HTTPS);
Reference ref = new Reference("https://github.com/Qi4j/qi4j-sdk/commits/develop.atom");
ContextResourceClientFactory contextResourceClientFactory = module.newObject(ContextResourceClientFactory.class, client);
contextResourceClientFactory.registerResponseReader(new ResponseReader() {
@Override
public Object readResponse(Response response, Class<?> resultType) throws ResourceException {
if (resultType.equals(Document.class)) {
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(false);
return documentBuilderFactory.newDocumentBuilder().parse(response.getEntity().getStream());
} catch (Exception e) {
throw new ResourceException(e);
}
}
return null;
}
});
contextResourceClientFactory.setErrorHandler(new ErrorHandler().onError(ErrorHandler.RECOVERABLE_ERROR, new ResponseHandler() {
@Override
public HandlerCommand handleResponse(Response response, ContextResourceClient client) {
System.out.println(">> REFRESH on recoverable error: " + response.getStatus());
return refresh();
}
}));
crc = contextResourceClientFactory.newClient(ref);
crc.onResource(new ResultHandler<Document>() {
Iterator<Node> itemNodes;
@Override
public HandlerCommand handleResult(Document result, ContextResourceClient client) {
try {
final XPath xPath = XPathFactory.newInstance().newXPath();
System.out.println("== " + xPath.evaluate("feed/title", result) + " ==");
final NodeList nodes = (NodeList) xPath.evaluate("feed/entry", result, XPathConstants.NODESET);
List<Node> items = new ArrayList<>();
for (int i = 0; i < nodes.getLength(); i++) {
items.add(nodes.item(i));
}
itemNodes = items.iterator();
return processEntry(xPath);
} catch (XPathExpressionException e) {
throw new ResourceException(e);
}
}
private HandlerCommand processEntry(final XPath xPath) throws XPathExpressionException {
if (!itemNodes.hasNext()) {
return null;
}
Node item = itemNodes.next();
String title = xPath.evaluate("title", item);
String detailUrl = xPath.evaluate("link/@href", item);
System.out.println("-- " + title + " --");
System.out.println("-- " + detailUrl + " --");
return processEntry(xPath);
}
});
crc.start();
}
use of javax.xml.parsers.DocumentBuilderFactory in project tinker by Tencent.
the class AndroidParser method parse.
private void parse() throws ParserException {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
Document document;
try {
DocumentBuilder builder = builderFactory.newDocumentBuilder();
document = builder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
Node manifestNode = document.getElementsByTagName("manifest").item(0);
NodeList nodes = manifestNode.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
String nodeName = node.getNodeName();
if (nodeName.equals("application")) {
NodeList children = node.getChildNodes();
for (int j = 0; j < children.getLength(); j++) {
Node child = children.item(j);
String childName = child.getNodeName();
switch(childName) {
case "service":
services.add(getAndroidComponent(child, TYPE_SERVICE));
break;
case "activity":
activities.add(getAndroidComponent(child, TYPE_ACTIVITY));
break;
case "receiver":
receivers.add(getAndroidComponent(child, TYPE_BROADCAST_RECEIVER));
break;
case "provider":
providers.add(getAndroidComponent(child, TYPE_CONTENT_PROVIDER));
break;
case "meta-data":
NamedNodeMap attributes = child.getAttributes();
metaDatas.put(getAttribute(attributes, "android:name"), getAttribute(attributes, "android:value"));
break;
}
}
}
}
} catch (Exception e) {
throw new ParserException("Error parsing AndroidManifest.xml", e);
}
}
use of javax.xml.parsers.DocumentBuilderFactory 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.DocumentBuilderFactory 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.DocumentBuilderFactory 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)));
}
Aggregations