InputStream型のxmlから欲しいノード名や属性やらを抽出
今日は,二回目の更新になります.これも私用で残しおきたかったので載せます.
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; Process proce; Runtime rt = Runtime.getRuntime(); proce = rt.exec("コマンド"); InputStream is = proce.getInputStream();// 標準出力を取得 // xml をパース DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = factory.newDocumentBuilder(); Document document = documentBuilder.parse(is);; Element root = document.getDocumentElement(); //ルート要素のノード名を取得する System.out.println("ノード名:" +root.getNodeName()); //ルート要素の属性を取得する System.out.println("ルート要素の属性:" + root.getAttribute("link")); //ルート要素の子ノードを取得する NodeList rootChildren = root.getChildNodes(); System.out.println("子要素の数:" + rootChildren.getLength()); 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("chunk")) { System.out.println("link:" + element.getAttribute("link")); NodeList ChunkChildren = node.getChildNodes(); for (int j=0; j < ChunkChildren.getLength(); j++) { Node personNode = ChunkChildren.item(j); if (personNode.getNodeType() == Node.ELEMENT_NODE) { if (personNode.getNodeName().equals("tok"))System.out.println("単語:" + personNode.getTextContent()); } } System.out.println("------------------"); } } }xmlは,参考サイトのものを組み込んだだけです.これでだいぶ助かりました.ありがとうございます.