どうしたわけか、文字列でXMLを扱う必要がありまして、
取りあえず書いてみた。
細かい説明は後日。
こんなXMLを作ってパース。
<root> <devil> <misician no="0">小暮伝衛門</misician> <album no="0">好色萬声男</album> </devil> <devil> <misician no="1">CANTA</misician> <album no="1">流星と春の嵐</album> </devil> </root>
package com.fudebaco.util; import com.fudebaco.bean.SampleXmlBean; import java.io.ByteArrayInputStream; import java.io.StringWriter; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class XmlSample { public static void main(String args[]){ List<SampleXmlBean> list = createSampleList(); String xml = createStringXml(list); try { parseXml(xml); } catch (Exception ex) { Logger.getLogger(XmlSample.class.getName()).log(Level.SEVERE, null, ex); } } private static String createStringXml(List<SampleXmlBean> list) { String xml = ""; try { DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docbuilder = dbfactory.newDocumentBuilder(); Document document = docbuilder.newDocument(); // ノード生成 Element root = document.createElement("root"); // ノードをDocumentに追加 document.appendChild(root); for (int i = 0; i < list.size(); i++) { SampleXmlBean bean = list.get(i); Element sep = document.createElement("devil"); root.appendChild(sep); Element elem1 = document.createElement("misician"); elem1.setTextContent(bean.getElem1()); elem1.setAttribute("no", Integer.toString(i)); sep.appendChild(elem1); Element elem2 = document.createElement("album"); elem2.setTextContent(bean.getElem2()); elem2.setAttribute("no", Integer.toString(i)); sep.appendChild(elem2); } /* * DOMオブジェクトを文字列として出力 */ StringWriter sw = new StringWriter(); TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer transformer = tfactory.newTransformer(); transformer.setOutputProperty("method", "html"); // 宣言を出さない // transformer.transform(new DOMSource(document), new StreamResult(System.out)); transformer.transform(new DOMSource(document), new StreamResult(sw)); xml = sw.toString(); } catch (Exception e) { // TODO 自動生成された catch ブロック e.printStackTrace(); } return xml; } private static void parseXml(String xml) throws Exception { DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docbuilder = dbfactory.newDocumentBuilder(); // Document document = documentBuilder.parse(file); Document document = docbuilder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8"))); // ルートの要素を取得 Element root = document.getDocumentElement(); System.out.println("ルート:" + root.getNodeName()); //ルートの子ノードを取得 NodeList rootChildren = root.getChildNodes(); for(int i=0; i < rootChildren.getLength(); i++) { Node node = rootChildren.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element)node; if (element.getNodeName().equals("devil")) { NodeList devilChildren = node.getChildNodes(); for (int j=0; j < devilChildren.getLength(); j++) { Node devilNode = devilChildren.item(j); if (devilNode.getNodeType() == Node.ELEMENT_NODE) { if (devilNode.getNodeName().equals("misician")) { System.out.println("misician:" + devilNode.getTextContent()); } } } } } } } private static List<SampleXmlBean> createSampleList() { List<SampleXmlBean> list = new ArrayList<>(); SampleXmlBean bean1 = new SampleXmlBean(); bean1.setElem1("小暮伝衛門"); bean1.setElem2("好色萬声男"); list.add(bean1); SampleXmlBean bean2 = new SampleXmlBean(); bean2.setElem1("CANTA"); bean2.setElem2("流星と春の嵐"); list.add(bean2); return list; } }
参考にさせていただいたサイト
[blogcard url=”http://www.atmarkit.co.jp/ait/articles/0102/10/news003_3.html″]
[blogcard url=”http://code-examples-ja.hateblo.jp/entry/2014/08/08/Java_XML%E3%82%92DOM%E3%81%A7%E8%AA%AD%E3%81%BF%E8%BE%BC%E3%82%93%E3%81%A7%E5%86%85%E5%AE%B9%E3%82%92%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B″]