<p><span style="font-size: 24px;">这个星期准备考java二级了,但是java 书还没有学完,希望能顺利过吧。
刚看到java 上讲到xml文本,于是研究了一下xml格式的要求以及解析方式,上网看了看资料,其实解析xml的方式有很多,基本主流的就是 dom4j和rome等开源类库,dom4j是占用内存较小的,属于一边解析,一边读取,而rome则是一次性将xml读取到内存当中。对于小型的xml文本,用rome可能更方便一点吧,我用了rome形式来解析,研究了一下api,于是弄了个swing界面来显示解析的结果,并且把每条标题用label的形式储存,然后给label设置了超链接,点击之后能够用桌面的浏览器打开xml解析的link连接。而旁边的关键词搜索,则能够找到包含关键字的全部内容。</span></p><p><span style="font-size: 24px;"></span></p>
package testxml;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.zip.GZIPInputStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
public class SwingXml extends JFrame {
// JTextArea textArea = new JTextArea(10, 20);
JTextField textField = new JTextField(10);
JButton button = new JButton("访问");
JLabel label = new JLabel("rss地址:");
List result;
public SwingXml() {
super("尼克明Rss阅读器");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(750, 500);
setLayout(new BorderLayout());
JPanel panel4 = new JPanel(new GridLayout(25, 1));
JScrollPane scrollPane = new JScrollPane(panel4);
add(scrollPane, BorderLayout.CENTER);
JPanel panel = new JPanel(new FlowLayout());
JButton buttons = new JButton("退出");
panel.add(label);
panel.add(textField);
panel.add(button);
panel.add(buttons);
buttons.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
textField.setText("http://rss.sina.com.cn/news/marquee/ddt.xml");
// textArea.setEditable(false);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
try {
panel4.removeAll();
String address = textField.getText();
URL url = new URL(address);
SyndFeedInput syndFeedInput = new SyndFeedInput();
SyndFeed feed = null;
URLConnection connection = url.openConnection();
// 然后抓包测试,如果是zip格式的包,则比较省流量
String string = connection
.getHeaderField("Content-Encoding");
if (string != null && string.contains("gzip")) {
System.out.println("包含格式是一个包");
GZIPInputStream gzipInputStream = new GZIPInputStream(
connection.getInputStream());
feed = syndFeedInput.build(new XmlReader(
gzipInputStream));
} else {
feed = syndFeedInput.build(new XmlReader(connection
.getInputStream()));
}
result = feed.getEntries();
for (int i = 0; i < result.size(); i++) {
SyndEntry syndEntry = (SyndEntry) result.get(i);
JLabel label6 = new JLabel(syndEntry.getTitle().trim());
label6.setForeground(Color.blue);
label6.setFont(new Font("微软雅黑", 0, 16));
label6.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
label6.setForeground(Color.blue);
label6.setFont(new Font("微软雅黑", 0, 16));
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
label6.setForeground(Color.gray);
label6.setToolTipText("fsdf");
if (!syndEntry.getDescription().getValue()
.contains(",")) {
label6.setToolTipText(syndEntry
.getDescription().getValue().trim());
} else {
String[] tempString = syndEntry
.getDescription().getValue().trim()
.split(",");
label6.setToolTipText(tempString[0]
+ tempString[1] + "......");
}
}
@Override
public void mouseClicked(MouseEvent e) {// 显示网页
URI url2 = null;
try {
url2 = new URI(syndEntry.getLink());
} catch (URISyntaxException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
Desktop desktop = Desktop.getDesktop();// 桌面
if (Desktop.isDesktopSupported()
&& desktop
.isSupported(Desktop.Action.BROWSE)) {
try {
desktop.browse(url2);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
panel4.add(label6);
panel4.updateUI();
// textArea.append("" + syndEntry.getTitle().trim()
// + "\n\n");本来是panel来显示
}
} catch (Exception e) {
// TODO: handle exception
}
}
});
add(panel, BorderLayout.SOUTH);
JPanel panel2 = new JPanel(new FlowLayout());
JLabel label2 = new JLabel("请输入关键字:");
JTextField textField2 = new JTextField(5);
JButton button2 = new JButton("搜索");
panel2.add(label2);
panel2.add(textField2);
panel2.add(button2);
JPanel panel3 = new JPanel(new BorderLayout());
panel3.add(panel2, BorderLayout.NORTH);
JTextArea textArea3 = new JTextArea();
textArea3.setEditable(false);
textArea3.setLineWrap(true);
JScrollPane scrollPane2 = new JScrollPane(textArea3);
panel3.add(scrollPane2, BorderLayout.CENTER);
add(panel3, BorderLayout.EAST);
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
try {
textArea3.setText("");
String key = textField2.getText();
for (int i = 0; i < result.size(); i++) {
SyndEntry syndEntry = (SyndEntry) result.get(i);
if (syndEntry.getTitle().contains(key)) {
textArea3.append("标题:"
+ syndEntry.getTitle().trim()
+ "\n\n"
+ "简介:"
+ syndEntry.getDescription().getValue()
.trim() + "\n\n" + "链接地址:"
+ syndEntry.getLink().trim() + "\n\n\n");
} else {
if (i == result.size() - 1
&& textArea3.getText().equals("")) {
textArea3.setText("我tm找不到相关结果!");
}
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
});
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingXml s = new SwingXml();
}
}
<img src="https://img-blog.csdn.net/20141229161259596?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzk4Mjc2OQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />