2017-09-29 7 views
0

私はdbFactoryを使ってXMLファイルからデータを読み込もうとしていますが、必ずしもそこにない属性(「イメージ」)を見つけるのに苦労しています。以下のデータ:ここ珍しい属性を見つける

は私がストリームで試してみました

<screen> 
      <screenID>step_5</screenID> 
      <video>/video/Task 5 - Open Word.mp4</video> 
      <vid_caption>Task 5 - Open Word</vid_caption> 
      <image>/shared_images/word_icon.png</image> 
     </screen> 
     <screen> 
      <screenID>step_6</screenID> 
      <video>/video/Task 6 - How to open MS Word.mp4</video> 
      <vid_caption>Task 6 - How to open MS Word</vid_caption> 
     </screen> 

ファイルからいくつかのデータであり、これは私が、私はシンプルな何かが欠けていていることを感じ、私の最後で、以下、私が作成したプログラムですそれのために

  //imports for XML readers 
    import javax.xml.parsers.DocumentBuilderFactory; 
    import javax.xml.parsers.DocumentBuilder; 
    import org.w3c.dom.Document; 
    import org.w3c.dom.NodeList; 
    import org.w3c.dom.Node; 
    import org.w3c.dom.Element; 
    import java.io.File; 
    import java.util.ArrayList; 
    import java.util.List; 

    public class ReadXML 
    { 

     public static void main(String args[]) 
     { 
      //try to read in the file 
      try 
      { 
    //create the file to read in 
    //File XmlFile = new File("Documents\\University\\2017\\Courses\\Second Semester\\CSC3003S\\Capstone\\Program\\Capstone-master\\elearnerselfstudy.xml"); 

    File XmlFile = new File("elearnerselfstudy.txt"); 
    //Defines a factory API that enables applications to obtain a parser that produces DOM object trees from XML documents. 
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
    Document document = dBuilder.parse(XmlFile); 

    //need to normalize - read the stack overflow 
    document.getDocumentElement().normalize(); 

    //print out root for testing purposes 
    System.out.println("The root element is :" + document.getDocumentElement().getNodeName() + "\n"); 

    //list of lessons - it is reading the elements into the list fine 
    NodeList nLessonList = document.getElementsByTagName("lesson"); 
    System.out.println("I have the lesson list ready"); 
    System.out.println("The length of the lessonList is: " + nLessonList.getLength()+"\n"); 

    //list for the screens - it is reading the elements into the list fine, the error is somewhere else 
    NodeList nScreenList = document.getElementsByTagName("screen"); 
    System.out.println("I have the screen list ready"); 
    System.out.println("The length of the screenList is: " + nScreenList.getLength()+ "\n"); 

    //lesson list iteration 
    for (int temp = 0; temp < nLessonList.getLength(); temp++) 
    { 
     Node nNode = nLessonList.item(temp); 
     System.out.println("\nCurrent Element :" + nNode.getNodeName()); 

     if (nNode.getNodeType() == Node.ELEMENT_NODE) 
    { 
     //System.out.println("Are we even insdie the if bro - we definitely penetrate the if"); 
     //System.out.println("Still not a good enough reason to use the word penetrate" + "\n"); 

      Element eElement = (Element) nNode; 

     //System.out.println("Lesson : " + eElement.getAttribute("lesson")); 
     System.out.println("Lesson : " + eElement.getAttribute("lesson_title")); 
     System.out.println("Lesson ID : " + eElement.getAttribute("lesson_id")); 
     System.out.println("Lesson Type : " + eElement.getAttribute("lesson_type")); 



     }//end if 
    }//end for loop through tree 

    //screen list iteration 
    for (int temp = 0; temp < nScreenList.getLength(); temp++) 
    { 
     Node nNode2 = nScreenList.item(temp); 
     System.out.println("\nCurrent Element :" + nNode2.getNodeName()); 

     if (nNode2.getNodeType() == Node.ELEMENT_NODE) 
    {    
      Element eElement = (Element) nNode2; 

     //return elements 
     //System.out.println("Screen : " + eElement.getAttribute("screen")); 
     System.out.println("ScreenId is : " + eElement.getElementsByTagName("screenID").item(0).getTextContent()); 
     System.out.println("Video is : " + eElement.getElementsByTagName("video").item(0).getTextContent()); 
     System.out.println("Video Caption is : " + eElement.getElementsByTagName("vid_caption").item(0).getTextContent()); 

     if (eElement.getAttributeNode("image")!=null)//(eElement.hasAttribute("image")==true) 
     { 

      System.out.println("Image is : " + eElement.getElementsByTagName("image").item(0).getTextContent()); 
     } 


    }//end if 
    }//end for list iteration 
}//end try 

//catch 
catch (Exception e) 
{ 
    e.printStackTrace(); 
}//end catch 
    }//end main 
    }//end class 
+0

あなたのXMLマークアップによると、 'image'は要素であり、属性ではありません。 – Alohci

答えて

0
//get the image path if it is there 
if (eElement.getElementsByTagName("image").item(0)!=null) 
{ 
    System.out.println("Image path is : " +eElement.getElementsByTagName("image").item(0).getTextContent()); 
} 
関連する問題