admin 管理员组文章数量: 1184232
2024年3月7日发(作者:tcp和socket区别)
package ser;import ;import ntBuilder;import ntBuilderFactory;import nt;import t;import ;import st;public class XMLParser {public void getAllUserNames(String fileName) {try {DocumentBuilderFactory dbf = tance();DocumentBuilder db = umentBuilder();File file = new File(fileName);if (()) {Document doc = (file);Element docEle = umentElement();// Print root element of the n(“Root element of the document: ”+ eName());NodeList studentList = mentsByTagName(“student”);// Print total student elements in n(“Total students: ” + gth());if (studentList != null && gth() > 0) {for (int i = 0; i < gth(); i++) {Node node = (i);if (eType() == T_NODE) {n(“=====================”);Element e = (Element) node;NodeList nodeList = mentsByTagName(“name”);n(“Name: ”+ (0).getChildNodes().item(0).getNodeValue());nodeList = mentsByTagName(“grade”);n(“Grade: ”+ (0).getChildNodes().item(0).getNodeValue());nodeList = mentsByTagName(“age”);n(“Age: ”+ (0).getChildNodes().item(0).getNodeValue());}}} else {(1);}}} catch (Exception e) {n(e);}}public static void main(String[] args) {XMLParser parser = new XMLParser();UserNames(“c:”);}}17. 把 Array 转换成 Map
import ;import tils;public class Main {public static void main(String[] args) {String[][] countries = { { ”United States”, ”New York” }, { ”United Kingdom”, ”London” },{ ”Netherland”, ”Amsterdam” }, { ”Japan”, ”Tokyo” }, { ”France”, ”Paris” } };Map countryCapitals = (countries);n(“Capital of Japan is ” + (“Japan”));n(“Capital of France is ” + (“France”));}}18. 发送邮件import .*;import et.*;import .*;public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException{boolean debug = false;//Set the host smtp addressProperties props = new Properties();(“”, ””);// create some properties and get the default SessionSession session = aultInstance(props, null);ug(debug);// create a messageMessage msg = new MimeMessage(session);// set the from and to addressInternetAddress addressFrom = new InternetAddress(from);m(addressFrom);InternetAddress[] addressTo = new InternetAddress[];for (int i = 0; i < ; i++){addressTo[i] = new InternetAddress(recipients[i]);}ipients(, addressTo);// Optional : You can also set your custom headers in the Email if you der(“MyHeaderName”, ”myHeaderValue”);// Setting the Subject and Content ject(subject);tent(message, ”text/plain”);(msg);}19. 发送代数据的HTTP 请求
版权声明:本文标题:20个常用的经典JAVA代码片段 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1709756676a545339.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论