HiiHahWIKI - making some notes for... -
javaでDOM(1)
をテンプレートにして作成
開始行:
[[javaでXpathを使う(1)]]
package example.ex3;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class XMLSample1 {
private static final String IN_FILENAME="TestFileNam...
private static final String OUT_FILENAME="OUT.xml";
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
try {
// DocumentBuilderFactory インスタンスを取得
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true); // never ...
File inputFile = new File(IN_FILENAME);
BufferedReader br = new BufferedReader(new...
StringBuffer in = new StringBuffer();
String tmp;
while ((tmp = br.readLine()) != null){
in.append(tmp);
}
InputSource is = new InputSource(new Strin...
// DocumentBuilder を取得
DocumentBuilder builder =
factory.newDocumentBuilder();
// DocumentBuilder を用いて Document オブ...
// Document doc = builder.parse(new File(IN...
Document doc = builder.parse(is);
XPathFactory xPathFactory = XPathFactory.n...
XPath xpath = xPathFactory.newXPath();
XPathExpression expr = xpath.compile("//bo...
// XPathExpression expr = xpath.compile("//...
Object result = expr.evaluate(doc, XPathCo...
NodeList nodeList = (NodeList) result;
for (int i = 0; i < nodeList.getLength(); ...
System.out.println(nodeList.item(i).ge...
// doc.removeChild(nodeList.item(i));
}
// System.out.println(doc.);
StringWriter sw = new StringWriter();
TransformerFactory tff=TransformerFactory....
Transformer tf = tff.newTransformer();
tf.setOutputProperty("encoding", "UTF-8");
tf.transform( new DOMSource(doc), new Stre...
System.out.println(sw);
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (XPathExpressionException e) {
e.printStackTrace();
} catch (TransformerException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
}
}
終了行:
[[javaでXpathを使う(1)]]
package example.ex3;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class XMLSample1 {
private static final String IN_FILENAME="TestFileNam...
private static final String OUT_FILENAME="OUT.xml";
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
try {
// DocumentBuilderFactory インスタンスを取得
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true); // never ...
File inputFile = new File(IN_FILENAME);
BufferedReader br = new BufferedReader(new...
StringBuffer in = new StringBuffer();
String tmp;
while ((tmp = br.readLine()) != null){
in.append(tmp);
}
InputSource is = new InputSource(new Strin...
// DocumentBuilder を取得
DocumentBuilder builder =
factory.newDocumentBuilder();
// DocumentBuilder を用いて Document オブ...
// Document doc = builder.parse(new File(IN...
Document doc = builder.parse(is);
XPathFactory xPathFactory = XPathFactory.n...
XPath xpath = xPathFactory.newXPath();
XPathExpression expr = xpath.compile("//bo...
// XPathExpression expr = xpath.compile("//...
Object result = expr.evaluate(doc, XPathCo...
NodeList nodeList = (NodeList) result;
for (int i = 0; i < nodeList.getLength(); ...
System.out.println(nodeList.item(i).ge...
// doc.removeChild(nodeList.item(i));
}
// System.out.println(doc.);
StringWriter sw = new StringWriter();
TransformerFactory tff=TransformerFactory....
Transformer tf = tff.newTransformer();
tf.setOutputProperty("encoding", "UTF-8");
tf.transform( new DOMSource(doc), new Stre...
System.out.println(sw);
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (XPathExpressionException e) {
e.printStackTrace();
} catch (TransformerException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
}
}
ページ名: