実現したいこと
xmlの読み込みをしてgetElementsByTagNameメソッドでのnodelistの取得をしたい
getElementsByTagNameメソッドの引数に"rage"のように直接文字列をかけば、
取得できるが String変数を引数に渡すと取得ができない
取得できるコード
java
1import org.w3c.dom.Document; 2import org.w3c.dom.Element; 3import org.w3c.dom.NodeList; 4import org.xml.sax.SAXException; 5import javax.xml.parsers.DocumentBuilderFactory; 6import javax.xml.parsers.ParserConfigurationException; 7import java.io.*; 8 9 10//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or 11// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter. 12public class Main { 13 public static void main(String[] args) throws IOException { 14 15 InputStream heroInputStream = null; 16 InputStream movesInputStream = null; 17 try { 18 heroInputStream = new FileInputStream("C:\\java\\hero.xml"); 19 movesInputStream = new FileInputStream("C:\\java\\moveData.xml"); 20 Document heroDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(heroInputStream); 21 Document moveDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(movesInputStream); 22 Element rootHero = heroDoc.getDocumentElement(); 23 Element rootMoves = moveDoc.getDocumentElement(); 24 NodeList moves = rootHero.getElementsByTagName("moves"); 25 for(int i=0;i<moves.getLength();i++){ 26 Element move = (Element) moves.item(i); 27 String name = move.getTextContent(); 28 System.out.println(name); 29 NodeList nodeList = rootMoves.getElementsByTagName("Rage"); 30 Element moveData = (Element) nodeList.item(0); 31 System.out.println(moveData.getTextContent()); 32 } 33 34 } catch (FileNotFoundException e) { 35 throw new RuntimeException(e); 36 } catch (IOException e) { 37 throw new RuntimeException(e); 38 } catch (ParserConfigurationException e) { 39 throw new RuntimeException(e); 40 } catch (SAXException e) { 41 throw new RuntimeException(e); 42 } 43 } 44 }
取得できないコード
java
1import org.w3c.dom.Document; 2import org.w3c.dom.Element; 3import org.w3c.dom.NodeList; 4import org.xml.sax.SAXException; 5import javax.xml.parsers.DocumentBuilderFactory; 6import javax.xml.parsers.ParserConfigurationException; 7import java.io.*; 8 9 10//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or 11// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter. 12public class Main { 13 public static void main(String[] args) throws IOException { 14 15 InputStream heroInputStream = null; 16 InputStream movesInputStream = null; 17 try { 18 heroInputStream = new FileInputStream("C:\\java\\hero.xml"); 19 movesInputStream = new FileInputStream("C:\\java\\moveData.xml"); 20 Document heroDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(heroInputStream); 21 Document moveDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(movesInputStream); 22 Element rootHero = heroDoc.getDocumentElement(); 23 Element rootMoves = moveDoc.getDocumentElement(); 24 NodeList moves = rootHero.getElementsByTagName("moves"); 25 for(int i=0;i<moves.getLength();i++){ 26 Element move = (Element) moves.item(i); 27 String name = move.getTextContent(); 28 System.out.println(name); 29 NodeList nodeList = rootMoves.getElementsByTagName(name); 30 Element moveData = (Element) nodeList.item(0); 31 System.out.println(moveData.getTextContent()); 32 } 33 34 } catch (FileNotFoundException e) { 35 throw new RuntimeException(e); 36 } catch (IOException e) { 37 throw new RuntimeException(e); 38 } catch (ParserConfigurationException e) { 39 throw new RuntimeException(e); 40 } catch (SAXException e) { 41 throw new RuntimeException(e); 42 } 43 } 44 }
hero.xml
xml
1<hero> 2<status> 3<hp>10</hp> 4</status> 5<moves> 6<move1>HipDrop</move1> 7<move2>ElecKnee</move2> 8<move3>Bubble</move3> 9<move4>Rage</move4> 10</moves> 11</hero>
moveData.xml
xml
1<MoveData> 2<Rage><at>0</at> 3<name>rage</name> 4<type>mental</type> 5<pp>10</pp></Rage> 6<ElecKnee><at>20</at> 7<name>elecknee</name> 8<type>elec</type> 9<pp>2</pp></ElecKnee> 10<Bubble><at>5</at> 11<name>bubble</name> 12<type>water</type> 13<pp>20</pp></Bubble> 14<HipDrop><at>15</at> 15<name>hipdrop</name> 16<type>nomal</type> 17<pp>10</pp></HipDrop> 18</MoveData>
よろしくお願いします。
発生している問題・分からないこと
xml読み込みでのgetElementsByTagName()の挙動が想像通りに
実行されない
エラーメッセージ
error
1Exception in thread "main" java.lang.NullPointerException: Cannot invoke "org.w3c.dom.Element.getTextContent()" because "moveData" is null 2 at Main.main(Main.java:31)
該当のソースコード
特になし
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
プログラムの文字エンコードによるものかと思い
プログラム実行環境のエンコードを調べましたが
xmlの書式とおなじUTF-8でした
補足
特になし
回答1件
あなたの回答
tips
プレビュー
2024/07/24 03:20