1
我已經搜索並找到了許多可能的解決方案,但是我還沒有得到任何解決方案。我需要拉下一個XML並將其保存到光盤,然後調用它並從中拉出一個元素值。我已成功將XML保存到光盤,但現在我無法從單個元素中獲取值。我發現了一堆循環和數組,以獲得不同論壇中的所有數據,但我似乎失敗了。Java - 獲取單個XML值
任何幫助將不勝感激,我知道我有一堆額外的重複進口,它只是試圖這麼多事情混亂。謝謝!!!
的XML:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<licenseActivated>
\t \t <licenseKey>654</licenseKey>
\t \t <expirationDate>10/31/2016</expirationDate>
\t \t <uniqueId>Unique ID</uniqueId>
</licenseActivated>
//
// \t \t Sample License App for COMP394
//
// Import statements
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.net.URLConnection;
import java.io.InputStream;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import javax.xml.xpath.*;
import org.xml.sax.InputSource;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
class LicenseApp {
\t
public static void main(String[] args) {
LicenseApp http = new LicenseApp();
\t \t System.out.println("Key Verifyication!!!");
\t \t
\t \t http.sendkey();
\t \t //while(http.validation2() == false)
\t \t //{
\t \t \t //System.out.println("Key is wrong");
\t \t //}
\t \t
\t \t //System.out.println("Confgrats, the key is good!!! \nContinue with your program!!!");
}
\t
\t private boolean sendkey() {
\t \t
\t \t Scanner user_input = new Scanner(System.in);
\t \t String key;
\t \t System.out.println("Please enter your key."); // Outputs the prompt for information
\t \t key = user_input.next();
\t \t boolean valid = false;
\t \t
\t \t DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
\t \t
\t \t try
\t \t {
\t \t \t
\t \t \t String urlString = "http://localhost/license_response.php?response=activate&license=" + key;
\t \t \t URL url = new URL(urlString);
\t \t \t URLConnection conn = url.openConnection();
\t \t \t DocumentBuilderFactory factory2 = DocumentBuilderFactory.newInstance();
\t \t \t DocumentBuilder builder = factory2.newDocumentBuilder();
\t \t \t Document doc = builder.parse(conn.getInputStream());
// \t \t \t doc.getDocumentElement().normalize();
\t \t \t TransformerFactory factory3 = TransformerFactory.newInstance();
\t \t \t Transformer xform = factory3.newTransformer();
\t \t \t //xform.transform(new DOMSource(doc), new StreamResult(System.out)); // Outputs to screen
\t \t \t DOMSource source = new DOMSource(doc);
\t \t \t StreamResult output = new StreamResult(new File("C:\\school\\COMP394\\Project\\Java.Program\\license.xml"));
\t \t \t xform.transform(source, output); // Saves to file
\t \t \t
\t \t }
\t \t
\t \t catch(Exception e)
\t \t {
\t \t \t
\t \t }
\t \t
\t \t return valid; // Return boolean value
\t \t
\t }
\t
}
你的意思是「文件」,而不是「光盤」? –