简体   繁体   English

尝试将属性/值(例如XML文件的名称或日期)从目录引入Java

[英]Trying to bring the properties/values such as the names or dates of an XML file into java from a directory

Trying to bring the properties/values such as the names or dates of an XML file into java from a directory. 尝试将属性/值(例如XML文件的名称或日期)从目录引入Java。 but the problem is i can only bring in the names of the files such as Employee or Dates but cannot bring in the elements inside the file 但是问题是我只能带入员工或日期之类的文件名,而不能带入文件中的元素

  public class ProcessXML {
      public static void main2(String[] args) {

      File f = null;
      File[] paths;

      try{      
         // file
         f = new File("/Users/Adrian/Dropbox/XML.xml");

         // array of files and directory
         paths = f.listFiles();

         // for each name in the path array
         for(File path:paths)
         {
             path.isDirectory();
            // prints filename and directory name
            System.out.println(path);
         }
      }catch(Exception e){
         // if any error occurs
         e.printStackTrace();
      }
    }

   public static void main(String[] args) {

          File f = null;
          try{      
             // file
             f = new File("Users/Adrian/Dropbox/XML.xml");
             //other file
            //f = new File("/Users/Adrian/Dropbox/");
             listFile(f, "    ");
          }catch(Exception e){
             // if any error occurs
             e.printStackTrace();
          }
       }

    private static void listFile(final File file, final String indent) throws IOException, ParserConfigurationException, SAXException {
       if (file.isFile()) {
           if (file.getName().endsWith(".xml")) {
               System.out.println(indent + "File " + file.getName());
      //                   final InputStream is = new FileInputStream(file);
               processXML(file);
            }
       } else if (file.isDirectory()) {
           System.out.println(indent + "Dir " + file.getName());
           final File[] children = file.listFiles();
           for (final File child : children) {
               listFile(child, indent + "    ");
           }
       }
     }

                                                                                                                                                                                       private static void processXML(final File file) throws 
       IOException,  ParserConfigurationException, SAXException {
       DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
       DocumentBuilder db = dbf.newDocumentBuilder(); 
       Document doc = db.parse(file);


   }

}

Trying to bring the properties/values such as the names or dates of an XML file into java from a directory.the problem i am now having is trying to get the xml values out of the xml files i am nout sure what way to about it so if anyone has examples or can tell me which way to go about it it would be very appreciated 试图将属性/值(例如XML文件的名称或日期)从目录引入Java。我现在遇到的问题是试图从xml文件中获取xml值,但我不确定该如何处理因此,如果有人有示例或可以告诉我该采取哪种方法,将不胜感激

My guess is that it can not find the file since you didn't include the extension to the xml file. 我的猜测是,由于您没有包括xml文件的扩展名,因此找不到文件。 try add .xml to the end of the file path and see if that works 尝试将.xml添加到文件路径的末尾,看看是否可行

 import java.io.File;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;


 public class XPathTest {

 public static void main(String args[]) {
      try {

 File Employee = new File("Employee.xml");
 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
 Document doc = dBuilder.parse(Employee);
 doc.getDocumentElement().normalize();

  System.out.println("root of xml file" + doc.getDocumentElement().getNodeName());
  NodeList nodes = doc.getElementsByTagName("Employee");
  System.out.println("==========================");

   for (int i = 0; i < nodes.getLength(); i++) {
   Node node = nodes.item(i);


      if (node.getNodeType() == Node.ELEMENT_NODE) {
      Element element = (Element) node;
      System.out.println("Employee Age: " + getValue("age", element));
      System.out.println("Employee Name: " + getValue("name", element));
     System.out.println("Employee Gender: " + getValue("gender", element));
     System.out.println("Employee Role: " + getValue("role", element));
     } 
     }
     }          catch (Exception ex) {
        ex.printStackTrace();
       }
     }

     private static String getValue(String tag, Element element) {
     NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes(); 
     Node node = (Node) nodes.item(0);
     return node.getNodeValue();
    }
    }


    went onto change everything and now it works, brought in xml values without a problem 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM