寻找罕见的属性

时间:2017-09-29 18:59:03

标签: java xml nodes elements w3c

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

这是文件中的一些数据

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

我尝试使用流,这是我的最后一个,我觉得我遗漏了一些简单的东西,下面是我为它创建的程序

public animals(red, black:string): void {
  $("#tablet").append(`
<div class="" style="`+(source === "sports" ? "display:none" : "display:inherit")+`">
            <div  class="table skychair"  id="tablesky">
              <div class="light chairsContainer listSliderContainer">
                <div class="file file-mouse inactive">
                      <i class="parrot parrot-hen-mouse"></i>
                        </div>
                          <div class="file file-right inactive"> 
                                 <i class="parrot parrot-hen-right"></i>  
                                 </div> 
                                  <div class="chairwrapper listSliderWrapper">    
                                  <ul class="glass glass-tabs list sliderList" id="chairglass"> 
                                     <li class="active" data-tab="plus-tab"><a>plus</a></li>  
                                       <li data-tab="sports-tab"><a>sports</a></li>  
                                         </ul> 
                                          </div>
                                          </div>
                                          </div>
          </div>
);

1 个答案:

答案 0 :(得分: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());
}