📄 xmlparser.java
字号:
/*
* Created on 2005-10-12
*
*/
package lbj.hahahaha;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* @author lbj
*
*/
public class XMLParser {
public static void main(String[] args) throws Exception {
XMLParser parser = new XMLParser();
String s = "乐酷在线欢迎您的到来!哈哈哈哈!活活咔咔叽里咕噜突突咕咕!";
s = URLEncoder.encode(s, "utf-8");
System.out.println(s);
Response res = parser
.parse("http://cmsms-cpmtinterface-app.8522.com:7080/SMSInterface/api/mtmsg.jsp?UserID=13521262171&SendNo=13521262171&ProviderName=LINGSHI&ProviderPassword=LING1010SHI&ProductID=D001010&Message="
+ s);
System.out.println(res.getStatus());
System.out.println(res.getDescription());
}
public Response parse(String url) {
try {
URL u = new URL(url);
return parse(u.openStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public Response parse(InputStream is) {
try {
Response res = new Response();
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(is);
Node root = document.getFirstChild();
NodeList nodeList = root.getChildNodes();
int count = nodeList.getLength();
Node node = null;
String nodeName = null;
for (int i = 0; i < count; i++) {
node = nodeList.item(i);
nodeName = node.getNodeName();
//Status
if (nodeName.equalsIgnoreCase("status")) {
res.setStatus(Integer.parseInt(node.getChildNodes().item(0)
.getNodeValue()));
}
//Description
else if (nodeName.equalsIgnoreCase("description")) {
res.setDescription(node.getChildNodes().item(0)
.getNodeValue());
}
}
return res;
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -