始めに
java言語の習得を試みている者です。参考書を元に勉強しています。よって、プロのとの関わりがありません。つまり、専門用語や通な構文などは、全く知らない初心者です。お手柔らかにお願いします。
また、とあるサイトを参考にコードを書かせていただきました。末尾にそのサイトのURLを載せさせていただきます。
本文
java言語を習得するためだけのプログラムを書きました。タイトルにあるように、XML形式のファイルを読み書きするプログラムとなっています。主観ではありますが、長めのプログラムとなります。お付き合いいただけたら、幸いです。
java言語
1import org.w3c.dom.Document; 2import javax.xml.parsers.DocumentBuilder; 3import javax.xml.parsers.DocumentBuilderFactory; 4import org.w3c.dom.Element; 5import java.io.FileInputStream; 6import java.io.File; 7import javax.xml.transform.Transformer; 8import javax.xml.transform.TransformerFactory; 9import javax.xml.transform.dom.DOMSource; 10import javax.xml.transform.stream.StreamResult; 11import org.w3c.dom.NodeList; 12import java.io.IOException; 13import javax.xml.transform.TransformerException; 14import javax.xml.parsers.ParserConfigurationException; 15import org.xml.sax.SAXException; 16 17public class Second{ 18 public static void main(String[] args){ 19 FileInputStream fis = null; 20 byte b = 0; 21 File f = null; 22 try{ 23 f = new File("test.xml"); 24 if(!(f.exists())){f.createNewFile();} 25 }catch(IOException e){error(e);} 26//書く 27 try{ 28 fis = new FileInputStream("test.xml"); 29 DocumentBuilderFactory dbf = null; 30 DocumentBuilder db = null; 31 Document d = null; 32 try{ 33 dbf = DocumentBuilderFactory.newInstance(); 34 db = dbf.newDocumentBuilder(); 35 d = db.parse(fis); 36 }catch(ParserConfigurationException e){ 37 error(e); 38 b++; 39 }catch(SAXException e){ 40 error(e); 41 b++; 42 }if(b == 0){ 43 Element one = d.createElement("hero1"); 44 one.setAttribute("main","true"); 45 one.appendChild(d.createTextNode("クリア")); 46 Element two = d.createElement("hero2"); 47 two.setAttribute("main","true"); 48 two.appendChild(d.createTextNode("クリア")); 49 Element three = d.createElement("hero3"); 50 three.setAttribute("main","false"); 51 three.appendChild(d.createTextNode("クリア")); 52 d.appendChild(one); 53 d.appendChild(two); 54 two.appendChild(three); 55 try{ 56 TransformerFactory tff = TransformerFactory.newInstance(); 57 Transformer tf = tff.newTransformer(); 58 tf.setOutputProperty("indent","yes"); 59 tf.setOutputProperty("encoding","UTF-8"); 60 tf.transform(new DOMSource(d),new StreamResult(f)); 61 }catch(TransformerException e){error(e);} 62 }}catch(IOException e){error(e); 63 }finally{try{fis.close();}catch(Exception e){;}finally{b = 0;}} 64//読む 65 try{ 66 fis = new FileInputStream("test.xml"); 67 DocumentBuilderFactory dbf = null; 68 DocumentBuilder db = null; 69 Document d = null; 70 try{ 71 dbf = DocumentBuilderFactory.newInstance(); 72 db = dbf.newDocumentBuilder(); 73 d = db.parse(fis); 74 }catch(ParserConfigurationException e){ 75 error(e); 76 b++; 77 }catch(SAXException e){ 78 error(e); 79 b++; 80 }if(b == 0){ 81 Element hero = d.getDocumentElement(); 82 NodeList nl = hero.getChildNodes(); 83 for(int i = 0;i < nl.getLength();i++){ 84 if(nl.item(i) instanceof Element){ 85 Element e = (Element)nl.item(i); 86 System.out.println(e.getTextContent()); 87 } 88 } 89 } 90 }catch(IOException e){error(e); 91 }finally{ 92 try{fis.close();}catch(Exception e){;} 93 } 94 } 95 96 private static void error(Exception e){ 97 System.out.println("ーーーエラーメッセージーーー"); 98 System.out.println(e.getMessage()); 99 System.out.println("ーーースタックトレースーーー"); 100 e.printStackTrace(); 101 } 102}
コンパイルは成功しました。ただ、実行時にエラーが発生しました。下記のものとなります。
terminal
1[Fatal Error] :1:1: 途中でファイルの末尾に達しました。 2ーーーエラーメッセージーーー 3途中でファイルの末尾に達しました。 4ーーースタックトレースーーー 5org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 途中でファイルの末尾に達しました。 6 at java.xml/com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:261) 7 at java.xml/com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339) 8 at java.xml/javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:122) 9 at Second.main(Second.java:35) 10[Fatal Error] :1:1: 途中でファイルの末尾に達しました。 11ーーーエラーメッセージーーー 12途中でファイルの末尾に達しました。 13ーーースタックトレースーーー 14org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 途中でファイルの末尾に達しました。 15 at java.xml/com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:261) 16 at java.xml/com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339) 17 at java.xml/javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:122) 18 at Second.main(Second.java:73)
スタックトレースから、org.w3c.dom.Documentインタフェースのparseメソッドで発生していると推察し、このようなタイトルとしました。
最後に
XML形式のファイルに使用するorg.w3c.dom.Documentインタフェースのparseメソッドについて質問します。ただ、最終的な狙いは、上記のプログラムを動かすことにありますので、プログラムを動かす方法を教えていただきたいと存じます。
ご回答、よろしくお願いします。
参考サイトのURL

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/02/19 02:10
2022/02/19 07:14
2022/02/21 01:37