有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java发现一个稀有属性

我试图用dbFactory从XML文件中读取数据,并努力从下面的数据中找到一个不总是存在的属性(“图像”),该属性可以在步骤5中看到,然后在步骤6中看不到:

以下是文件中的一些数据

    <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>

我尝试过streams,这是我最后一次尝试,我觉得我错过了一些简单的东西,下面是我为它创建的程序

            //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

共 (1) 个答案

  1. # 1 楼答案

    //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());
    }