NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

package xml;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSInput;
import org.w3c.dom.ls.LSParser;

/** 検索語を含むタイトルの表示 (検索語を含んでいる場合に * * で囲む) */
public class TitleSearcherByXPath {
public static void main(String[] args) {
TitleSearcherByXPath searcher = new TitleSearcherByXPath();
String word = "首相";
try {
// InputStreamの用意
URL url = new URL("http://mainichi.jp/rss/etc/flash.rss");
URLConnection connection = url.openConnection();
connection.connect();
InputStream inputStream = connection.getInputStream();
// DOMツリーの構築
Document document = searcher.buildDocument(inputStream, "utf-8");
// リンクの表示
searcher.showTree(document.getDocumentElement(), word); // root要素を与える
}
catch (Exception e) {
e.printStackTrace();
}
}

/** DOM Tree の構築 */
public Document buildDocument(InputStream inputStream, String encoding) {
Document document = null;
try {
// DOM実装(implementation)の用意 (Load and Save用)
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS implementation = (DOMImplementationLS)registry.getDOMImplementation("XML 1.0");
// 読み込み対象の用意
LSInput input = implementation.createLSInput();
input.setByteStream(inputStream);
input.setEncoding(encoding);
// 構文解析器(parser)の用意
LSParser parser = implementation.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
// DOMの構築
document = parser.parse(input);
}
catch (Exception e) {
e.printStackTrace();
}
return document;
}

/**
* 検索語を目立たせてタイトルを表示
*
* @param node
* 処理対象のトップのノード
* @param word
* 検索語
*/
public void showTree(Node node, String word) {
for(Node t = node.getFirstChild(); t != null; t=t.getNextSibling()){
//if(t.getNodeValue().contains(word)){
System.out.println(t.getNodeValue());
//}
}
}
public ArrayList<Node> getItemList(Document document) {
ArrayList<Node> itemList = new ArrayList<Node>();
try {
// XPath の表現を扱う XPath オブジェクトを生成
XPathFactory factory = XPathFactory.newInstance();
XPath xPath = factory.newXPath();
// document に対して XPath を適用
// XPathConstants.NODESET は戻り値がノードの集合という意味
// RSS 1.0 (今回は名前空間は扱わないので rdf: は指定しない)
NodeList itemNodeList = (NodeList)xPath.evaluate("/RDF/item/title/",
document, XPathConstants.NODESET);
// RSS 2.0
//if(itemNodeList.getLength() == 0)
// itemNodeList = (NodeList)xPath.evaluate("/rss/channel/item/title",
// document, XPathConstants.NODESET);
// "//item" としても同じ結果が得られるが、無駄な探索が発生する
// なお、"//item" と getElementsByTagName("item") は同じ動作
// NodeList itemNodeList = document.getElementsByTagName("item");

// ノードリストの各 item要素から Item オブジェクトを生成
for(int i = 0; i < itemNodeList.getLength(); i++)
itemList.add(itemNodeList.item(i));
}
catch (DOMException e) {
System.err.println("DOMエラー:" + e);
}
catch (XPathExpressionException e) { // 追加
System.err.println("XPath 表現のエラー:" + e);
}
return itemList;
}


}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.