gpt4 book ai didi

Java - 获取单个 XML 值

转载 作者:行者123 更新时间:2023-11-30 07:49:10 25 4
gpt4 key购买 nike

我搜索并找到了许多可能的解决方案,但我还没有让其中任何一个起作用。我需要提取 XML 并将其保存到光盘,然后调用它并从中提取元素值。我已成功将 XML 保存到光盘,但现在无法从单个元素获取值。我发现了一堆循环和数组来获取不同论坛中的所有数据,但我似乎失败了。

任何帮助将不胜感激,我知道我有一堆额外的和重复的导入,尝试这么多事情只是一团糟。谢谢!!!

XML:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<licenseActivated>
<licenseKey>654</licenseKey>
<expirationDate>10/31/2016</expirationDate>
<uniqueId>Unique ID</uniqueId>
</licenseActivated>

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

public static void main(String[] args) {

LicenseApp http = new LicenseApp();

System.out.println("Key Verifyication!!!");

http.sendkey();
//while( http.validation2() == false)
//{
//System.out.println("Key is wrong");
//}

//System.out.println("Confgrats, the key is good!!! \nContinue with your program!!!");

}


private boolean sendkey() {

Scanner user_input = new Scanner(System.in);
String key;
System.out.println("Please enter your key."); // Outputs the prompt for information
key = user_input.next( );
boolean valid = false;

DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();

try
{

String urlString = "http://localhost/license_response.php?response=activate&license=" + key;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();

DocumentBuilderFactory factory2 = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory2.newDocumentBuilder();
Document doc = builder.parse(conn.getInputStream());
// doc.getDocumentElement().normalize();

TransformerFactory factory3 = TransformerFactory.newInstance();
Transformer xform = factory3.newTransformer();

//xform.transform(new DOMSource(doc), new StreamResult(System.out)); // Outputs to screen
DOMSource source = new DOMSource(doc);
StreamResult output = new StreamResult(new File("C:\\school\\COMP394\\Project\\Java.Program\\license.xml"));
xform.transform(source, output); // Saves to file





}

catch(Exception e)
{

}

return valid; // Return boolean value


}


}

最佳答案

谢谢,我终于找到了。

字符串 ID = doc.getElementsByTagName("uniqueId").item(0).getTextContent();

关于Java - 获取单个 XML 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33507188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com