Regex_1
Regex_1

Reputation: 21

Java - Get Single XML Value

I have searched and found many possible solutions, but I have not gotten any of them to work. I need to pull down an XML and save it to disc and then recall it and pull an elements value from it. I have successfully saved the XML to disc, but now I can't get the value from the single element. I found a bunch of loops and arrays to get all the data in different forums but I seem to be failing.

Any help would be appreciated, and I know I have a bunch of extra and repeating imports, it is just mess from trying so many things. Thanks!!!

The 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

		
	}
	

}

Upvotes: 1

Views: 100

Answers (1)

Regex_1
Regex_1

Reputation: 21

Thanks but I finally found it.

String ID = doc.getElementsByTagName("uniqueId").item(0).getTextContent();

Upvotes: 1

Related Questions