利用百度全网寻人这个平台的接口,练习一下如何解析收到的xml数据包
使用开源的dom4j对收到的xml数据包进行遍历
说句题外话,在解析过程中发现结果中的寻人信息中有些脑残在利用这个平台发布虚假的寻人启事进行娱乐,对于这种人真的不知道说什么好...
原文链接:https://www.f2er.com/xml/300565.htmlimport java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Scanner; import org.dom4j.*; public class DomDemo { public static LinkedList<String> name=new LinkedList<String>(); public static LinkedList<String> sex =new LinkedList<String>(); public static LinkedList<String> desc=new LinkedList<String>(); public static LinkedList<String> remarks=new LinkedList<String>(); public static LinkedList<String> phone=new LinkedList<String>(); public static void getElementList(Element e) { List elements=e.elements(); Info info=new Info(); if(elements.size()==0) { if(e.getName().equals("name")) { name.add(e.getTextTrim()); } else if(e.getName().equals("sex")) { sex.add(e.getTextTrim()); } else if(e.getName().equals("desc")) { desc.add(e.getTextTrim()); } else if(e.getName().equals("phone")) { phone.add(e.getTextTrim()); } else if(e.getName().equals("remarks")) { remarks.add(e.getTextTrim()); } //System.out.println(e.getTextTrim()); } else { Iterator it=elements.iterator(); while(it.hasNext()) { Element eL=(Element) it.next(); getElementList(eL); } } } public static void main(String[]args) { URL url; try { url = new URL("http://opendata.baidu.com/api.PHP?resource_id=6109&format=xml&ie=utf-8&oe=utf-8&query=%E5%9C%B0%E9%9C%87&from_mid=1&rn=10&pn=0"); HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); InputStreamReader input; input = new InputStreamReader(httpConn.getInputStream(),"utf-8"); Scanner inputStream=new Scanner(input); StringBuffer sb; sb=new StringBuffer(); while(inputStream.hasNext()) { sb.append(inputStream.nextLine()); } Document doc=DocumentHelper.parseText(sb.toString()); Element root=doc.getRootElement(); DomDemo.getElementList(root); Iterator iter=name.iterator(); Iterator iter1=sex.iterator(); Iterator iter2=desc.iterator(); Iterator iter3=remarks.iterator(); Iterator iter4=phone.iterator(); while(iter.hasNext()) { System.out.println("姓名:"+iter.next()); System.out.println("性别:"+iter1.next()); System.out.println("联系人:"+iter3.next()); System.out.println("联系人电话:"+iter4.next()); System.out.println("失踪者信息:"+iter2.next()+"\n"); } //System.out.println(doc); //System.out.println(sb.toString()); } catch (MalformedURLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.util.Iterator; import java.util.Scanner; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; public class Gui extends JFrame implements ActionListener { public static final int WIDTH=500; public static final int HEIGHT=600; public static final int LINES=5; public static final int CHAR_PER_LINE=30; private JTextField inputName; private JTextArea textDisplay; private static URL url; HttpURLConnection httpConn; public Gui() { setTitle("全网寻人"); setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); this.setLayout(new BorderLayout()); setSize(WIDTH,HEIGHT); Dimension scrSize=Toolkit.getDefaultToolkit().getScreenSize(); setLocation((scrSize.width-getSize().width)/2,(scrSize.height-getSize().height)/2); JPanel op=new JPanel(); op.setLayout(new FlowLayout()); inputName=new JTextField(30); op.add(inputName); JButton search=new JButton("Search"); search.addActionListener(this); op.add(search); textDisplay=new JTextArea(LINES,CHAR_PER_LINE); JScrollPane scrollpane=new JScrollPane(textDisplay); scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); add(scrollpane,BorderLayout.CENTER); add(op,BorderLayout.NORTH); initDisplay(); } public void initDisplay() { try { url = new URL("http://opendata.baidu.com/api.PHP?resource_id=6109&format=xml&ie=utf-8&oe=utf-8&query="+URLEncoder.encode("地震","utf-8")+"&from_mid=1&rn=10&pn=0"); httpConn= (HttpURLConnection) url.openConnection(); InputStreamReader input; input = new InputStreamReader(httpConn.getInputStream(),"utf-8"); Scanner inputStream=new Scanner(input); StringBuffer sb; sb=new StringBuffer(); while(inputStream.hasNext()) { sb.append(inputStream.nextLine()); } Document doc=DocumentHelper.parseText(sb.toString()); Element root=doc.getRootElement(); DomDemo.getElementList(root); Iterator iter=DomDemo.name.iterator(); Iterator iter1=DomDemo.sex.iterator(); Iterator iter2=DomDemo.desc.iterator(); Iterator iter3=DomDemo.remarks.iterator(); Iterator iter4=DomDemo.phone.iterator(); String content=""; while(iter.hasNext()) { content+="姓名:"+iter.next()+"\n"; content+="性别:"+iter1.next()+"\n"; content+="联系人:"+iter3.next()+"\n"; content+="联系人电话:"+iter4.next()+"\n"; content+="失踪者信息:"+iter2.next()+"\n\n"; } textDisplay.setText(content); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[]args) { Gui gui=new Gui(); gui.setVisible(true); } public void actionPerformed(ActionEvent e) { String actionCommand=e.getActionCommand(); if(actionCommand.equals("Search")) { try { String findName=URLEncoder.encode(inputName.getText().trim(),"utf-8"); url = new URL("http://opendata.baidu.com/api.PHP?resource_id=6109&format=xml&ie=utf-8&oe=utf-8&query="+findName+"&from_mid=1&rn=10&pn=0"); httpConn= (HttpURLConnection) url.openConnection(); InputStreamReader input; input = new InputStreamReader(httpConn.getInputStream(),"utf-8"); Scanner inputStream=new Scanner(input); StringBuffer sb; sb=new StringBuffer(); while(inputStream.hasNext()) { sb.append(inputStream.nextLine()); } Document doc=DocumentHelper.parseText(sb.toString()); Element root=doc.getRootElement(); DomDemo.clear(); DomDemo.getElementList(root); Iterator iter=DomDemo.name.iterator(); Iterator iter1=DomDemo.sex.iterator(); Iterator iter2=DomDemo.desc.iterator(); Iterator iter3=DomDemo.remarks.iterator(); Iterator iter4=DomDemo.phone.iterator(); String content=""; while(iter.hasNext()) { content+="姓名:"+iter.next()+"\n"; content+="性别:"+iter1.next()+"\n"; content+="联系人:"+iter3.next()+"\n"; content+="联系人电话:"+iter4.next()+"\n"; content+="失踪者信息:"+iter2.next()+"\n\n"; } textDisplay.setText(content); } catch (UnsupportedEncodingException e1) { } catch (MalformedURLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (DocumentException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } }