如何从 .jar 访问 xml 文件,以便从 php 函数执行 jar shell_exec


How to access a xml file from a .jar for execute jar from shell_exec php function

jar 来自带有 PHP 的 Apache 服务器。我用shell_exec,把我的罐子文件给她。

但是因为我在我的 java 类上解析了一个 xml 文件,所以我遇到了问题。Jar 无法访问 xml。

文件

样本萨克斯.java

package phpJavaPack;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class SampleSAX {
    public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {        
        /*int i=0;
        MyBufferedReaderWriter f = new MyBufferedReaderWriter();
        f.openRFile("dblp.xml");
        String sLine="";
        while ((i<10) && (sLine=f.readLine()) != null) {
            System.out.println(sLine);
            i++;
        }*/
        int i=0;     
        while(i<args.length){
        System.out.println("Argument's: " + args[0]);
        System.setProperty("entityExpansionLimit", "1000000");  
        SAXParserFactory spfac = SAXParserFactory.newInstance();
        spfac.setNamespaceAware(true);
        SAXParser saxparser = spfac.newSAXParser();

            MyHandler handler = new MyHandler(args[i]);

        InputSource is = new InputSource("dblpmini.xml");
        is.setEncoding("ISO-8859-1");
        System.out.println("Please wait...");
        saxparser.parse(is, handler);
        System.out.println("---->" + handler.getprofessorsPublications());
        i++;
        }
        //System.out.println("#####################################################################################################");
        //System.out.println("List of George A. Vouros: " + handler.getprofessorsPublicationsValue("George A. Vouros"));

        //System.out.println(handler.getProfessors());
        //handler.createHtmlPage();//emfanizei mia html selida me ta apotelesmata
    }
}

我的处理程序.java

package phpJavaPack;
import java.util.ArrayList;
import java.util.Hashtable;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class MyHandler extends DefaultHandler {
       private Publication publication;
       protected Hashtable<String, ArrayList<Publication>> professorsPublications = new Hashtable<String, ArrayList<Publication>>();
       private String temp ;
       private ArrayList<Publication> al;
       //private EntryLd entry = new EntryLd();
//       public MyHandler() {
//         super();
//         
//         String[] namesTable = entry.getNamesInput();
//         
//         
//         for (String name: namesTable) {
//             
//             al = new ArrayList<Publication>();
//             professorsPublications.put(name, al);
//         }
//         
//         System.out.println("HashTable: " + professorsPublications);
//       }
       public MyHandler(String authorForSearch){
           super();
           String name = authorForSearch;
           al = new ArrayList<Publication>();
           professorsPublications.put(name, al);
           System.out.println("HashTable: " + professorsPublications);
       }
       public Hashtable<String, ArrayList<Publication>> getprofessorsPublications() {
           return professorsPublications;
       }
       public ArrayList<Publication> getprofessorsPublicationsValue(String author) {
           return professorsPublications.get(author);
       }

       public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException 
       {
           temp = "";
           if (qName.equalsIgnoreCase("article") || qName.equalsIgnoreCase("inproceedings") 
                   || qName.equalsIgnoreCase("proceedings") || qName.equalsIgnoreCase("book") 
                   || qName.equalsIgnoreCase("incollection") || qName.equalsIgnoreCase("phdthesis")
                   || qName.equalsIgnoreCase("mastersthesis") || qName.equalsIgnoreCase("www")) {
               publication = new Publication();
           }
       }
       public void characters(char[] ch, int start, int length) {
           temp = new String(ch, start, length);
           System.out.println("----->>>" + temp);
           //System.out.print(" <--- My temp's start: " + temp + " :My temp's end --> ");
       }

       public void endElement(String uri, String localName, String qName) throws SAXException 
       {
           if (qName.equalsIgnoreCase("article") || qName.equalsIgnoreCase("inproceedings") 
                   || qName.equalsIgnoreCase("proceedings") || qName.equalsIgnoreCase("book") 
                   || qName.equalsIgnoreCase("incollection") || qName.equalsIgnoreCase("phdthesis")
                   || qName.equalsIgnoreCase("mastersthesis") || qName.equalsIgnoreCase("www")) 
           {
               for(int i=0; i<publication.getAuthors().size(); i++) {
                   String authorName = publication.getAuthors().get(i);
                   if(this.professorsPublications.containsKey(authorName)) {
                       this.publication.setType(localName);
                      this.professorsPublications.get(authorName).add(publication);
                   }
               }
           }
           if(qName.equalsIgnoreCase("author")) {
               publication.addAuthor(temp);
           }
           else if(qName.equalsIgnoreCase("title")) {
               publication.setTitle(temp);
           }
           else if(qName.equalsIgnoreCase("year")) {
               publication.setYear(Short.parseShort(temp));
           } 
           else if(qName.equalsIgnoreCase("booktitle")) {
               publication.setBooktitle(temp);
           }
           //String xl = publication.toString();
           //System.out.println(xl);
       }
 }

如何在可运行的罐子上正确给出 xml?

谢谢

请参阅 PHP exec() 命令:如何指定工作目录? - 很可能是最好的建议/示例proc_open

也请参阅该线程中的其他建议,例如 chdir