⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringxpath.java

📁 a Big Java source Code Exemples
💻 JAVA
字号:
import org.w3c.dom.*; 
import org.xml.sax.*;
import javax.xml.*; 
import javax.xml.parsers.*; 
import javax.xml.transform.*; 
import javax.xml.transform.dom.*; 
import javax.xml.transform.sax.*; 
import javax.xml.xpath.*; 
import javax.xml.namespace.*; 
import java.io.*;
import java.net.*;
import java.awt.*; 
import javax.swing.*; 
import javax.swing.text.*; 
import javax.swing.text.html.*; 
import java.util.*;

public class StringXPath{
	public static String evaluer(InputStream stream, String expression, 
									NamespaceContext namespace){
		String string = null;
		try{
			//cr閍tion de la source
			InputSource source = new InputSource(stream);
			
			//cr閍tion du XPath 
			XPathFactory fabrique = XPathFactory.newInstance();
			XPath xpath = fabrique.newXPath();
			if(namespace != null){
				xpath.setNamespaceContext(namespace);	
			}
			
			//関aluation de l'expression XPath
			XPathExpression exp = xpath.compile(expression);
			string = (String)exp.evaluate(source,XPathConstants.STRING);
			
		}catch(XPathExpressionException xpee){
			xpee.printStackTrace();
		}
		return string;
	}
	public static void main(String[] args){
		try{
			URL url = new URL("http://blog.developpez.com/xmlsrv/rss2.php?blog=1");
			
			String expression = "//item/content:encoded";
			NamespaceContext namespace = new NamespaceContext(){
				public String getNamespaceURI(String prefix){
					if("content".equals(prefix)){
						return "http://purl.org/rss/1.0/modules/content/";
					}else{
						return null;	
					}
				}
				public String getPrefix(String namespaceURI){
					if("http://purl.org/rss/1.0/modules/content/".equals(namespaceURI)){
						return "content";
					}else{
						return null;	
					}
				}
				public Iterator getPrefixes(String namespaceURI){
					return null;
				} 
			};
			
			String article = evaluer(url.openStream(),expression,namespace);
			
			JTextPane pane = new JTextPane();
			pane.setContentType("text/html");
			pane.setPreferredSize(new Dimension(200,200));
			JScrollPane scroll = new JScrollPane(pane);
			
			JFrame frame = new JFrame();
			frame.getContentPane().add(scroll);
			frame.pack();
			frame.setLocationRelativeTo(null);
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.setVisible(true);
			
			pane.setText(article);
			
		}catch(Exception e){
			e.printStackTrace();	
		}
	}	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -