In the past ive used xpath to find the value of specific nodes that came from an xml ducemnt from a URL. Now i want to use this same code but from an xml document that is stored locally on the android phone at say sdcard/images/xml/newxml.xml
Here is the old code that i would like to be able to implement to use this, i just cannot figure out how to use the local xml file instead of a URL.
URL url = new URL("UrlWentHere");
InputSource xml = new InputSource(url.openStream());
XPath xpath = XPathFactory.newInstance().newXPath();
datafromxml = xpath.evaluate("//forecast_conditions[1]/high/#data", xml);
I don't quite understand the question. Why not just URL url = new URL("sdcard/images/xml/newxml.xml"); - or does the problem have to do with the app's restricted access to the file system?
Related
I am performing simple RESTFUL service API verification in Java.
To handle response in JSON format is very convenient. Using org.json library, it's easy to convert JSON string from RESTFUL response into JSON object, and compare it with that of the expected JSON string.
JSONObject response = new JSONObject(json_response_str);
JSONObject expected = new JSONObject(json_expected_str);
JSONAssert.assertEquals(expected, response, JSONCompareMode.LENIENT);
If it is some element of the JSON response that need to compare, it is also easy because it is easy to extract sub element from JSONObject using APIs like:
JSONObject element_in_response = response.get("..."); or
JSONObject element_in_response = response.getJSONObject("...");
However, to handle response in XML format, things are more difficult. To compare the whole XML response with expected XML is not bad, I can use XMLUnit to do it:
String xml_response_str = ...
String xml_expected_str = ...
assertXMLEquals(xml_response_str, xml_expected_str);
However, there's no such things like xmlOject as there is in JSON.
So what do I do if want to compare some element of the XML response with expected?
I've search forums and JAXB is sometimes mentioned. I checked and it is about parsing XML to Java object. So am I supposed to parse both response XML string and expected XML string, then extract the element as Java object, then compare them? It seems complicated, not to mention I need the XML schema to start with.
What is the effective way to do this, is there anything that is as convenient as in the case of JSON?
Thanks,
You can try to use XPATH.
There is a short example.
Here is XML string:
<?xml version="1.0" encoding="UTF-8"?>
<resp>
<status>good</status>
<msg>hi</msg>
</resp>
The folowing code will get status and message:
String xml = "<resp><status>good</status><msg>hi</msg></resp>";
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
InputSource source = new InputSource(new StringReader(xml));
Document doc = (Document) xpath.evaluate("/", source, XPathConstants.NODE);
String status = xpath.evaluate("/resp/status", doc);
String msg = xpath.evaluate("/resp/msg", doc);
System.out.println("status=" + status);
System.out.println("Message=" + msg);
Here is more examples about how to use XPATH:
http://viralpatel.net/blogs/java-xml-xpath-tutorial-parse-xml/
There are a number of ways for testing XML. Converting XML to JSON not being one of them, but can be done.
Testing XML is usually performed using XPath style comparisons which focus on elements, attributes and content and not so much on comparing chunks.
From looking at your code you're already familiar with XML assertions from http://xmlunit.sourceforge.net/api/org/custommonkey/xmlunit/XMLAssert.htm but you might also want to look at http://www.w3schools.com/xsl/xpath_intro.asp.
XML validation is not that easy and does require a lot of effort to begin with. Once you've got your tesing tools in order it gets a whole lot easier.
verify (or extract) XML is independent from protocol, RESTful service, etc. , but it is normally used in SOAP services.
Comparison with JSON is interesting. JSON is more easy to use with php, javascript, ...
If you want to connect two java servers, XML is sufficient, or plain java Objects (not portable solution with other languages).
Better point to use XML: it is more, more powerfull, well standardized, and you have lot of tools to process it.
What you are asking: equivalent of JSONobject exists in XML for a while: it is a DOM document, or a Node.
1 read your XML
String xml="<root>content</root>";
DocumentBuilderFactory builderFactory =DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
// PARSE
Document document = builder.parse(new InputSource(new StringReader(xml)));
2 Best way to get some particular data: XPath: you give some path to your datas (root/group/class1/other_group/...), you can put wildcards (*), select about parameters, values, etc.
see this:
How to read XML using XPath in Java
XPath xpath = XPathFactory.newInstance().newXPath();
String expression="/root";
3 you can get direct values
expression="/root/text()";
String value = xpath.evaluate(expression, document);
4 or you get all data (if several)
XPathExpression expr = xpath.compile(expression) ;
NodeList nodes = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++)
{
Node nodeSegment = nodes.item(i);
if (nodeSegment.getNodeType() == Node.ELEMENT_NODE)
{
Element eElement = (Element) nodeSegment;
System.out.println("TAG="+eElement.getTagName());
System.out.println("VALUE="+eElement.getNodeValue());
I have an xml which web resource and I have String to URL where the xml is and in it I have to find specific tag someTag and to get its text content. How to do it in java?
I search but only find ways where there is specific XPath,but in my case I do not know where someTag can be. Which parser to use? And how to implement it?
An XPath expression starting with a double slash will find an element anywhere:
XPathExpression xpw = xpath.compile( "//someTag" );
I recently used transformer class to process stix xml messages.
http://docs.oracle.com/javase/7/docs/api/javax/xml/transform/Transformer.html
From Google app script (https://developers.google.com/apps-script/),I got this:-
var doc = DocumentApp.create('Hello, World');
// Access the body of the document, then add a paragraph.
doc.getBody().appendParagraph('This document was created by Google Apps Script.');
// Get the URL of the document.
var url = doc.getUrl();
What I would like to do is to be able to duplicate this from my javascript or java code so I can create a doc and get its URL. Any help is appreciated.
You'll need to use the Google Drive API.
I need upload a image into Google doc with google format in order to retrieving it back and saving the storage uasage as well. Below it my code sample, it was work fine but broken recently, What I got just a empty document. Could anyone help me?
DocsService client = new DocsService("testappv1");
client.setUserCredentials(username, password);
client.setProtocolVersion(DocsService.Versions.V2);
File file = new File("C:/test.jpg");
DocumentEntry newDocument = new DocumentEntry();
newDocument.setTitle(new PlainTextConstruct("test"));
String mimeType = DocumentListEntry.MediaType.fromFileName(file.getName()).getMimeType();
newDocument.setMediaSource(new MediaFileSource(file, mimeType));
newDocument = client.insert(destFolderUrl, newDocument);
Please have a look at this answer. Also, remove this line from your code:
client.setProtocolVersion(DocsService.Versions.V2);
You should use the default version (3.0) whenever possible.
I need to scrape a web page using Java and I've read that regex is a pretty inefficient way of doing it and one should put it into a DOM Document to navigate it.
I've tried reading the documentation but it seems too extensive and I don't know where to begin.
Could you show me how to scrape this table in to an array? I can try figuring out my way from there. A snippet/example would do just fine too.
Thanks.
You can try jsoup: Java HTML Parser. It is an excellent library with good sample codes.
Transform the web page you are trying to scrap into an XHTML document. There are several options to do this with Java, such as JTidy and HTMLCleaner. These tools will also automatically fix malformed HTML (e.g., close unclosed tags). Both work very well, but I prefer JTidy because it integrates better with Java's DOM API;
Extract required information using XPath expressions.
Here is a working example using JTidy and the Web Page you provided, used to extract all file names from the table.
public static void main(String[] args) throws Exception {
// Create a new JTidy instance and set options
Tidy tidy = new Tidy();
tidy.setXHTML(true);
// Parse an HTML page into a DOM document
URL url = new URL("http://www.cs.grinnell.edu/~walker/fluency-book/labs/sample-table.html");
Document doc = tidy.parseDOM(url.openStream(), System.out);
// Use XPath to obtain whatever you want from the (X)HTML
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("//td[#valign = 'top']/a/text()");
NodeList nodes = (NodeList)expr.evaluate(doc, XPathConstants.NODESET);
List<String> filenames = new ArrayList<String>();
for (int i = 0; i < nodes.getLength(); i++) {
filenames.add(nodes.item(i).getNodeValue());
}
System.out.println(filenames);
}
The result will be [Integer Processing:, Image Processing:, A Photo Album:, Run-time Experiments:, More Run-time Experiments:] as expected.
Another cool tool that you can use is Web Harvest. It basically does everything I did above but using an XML file to configure the extraction pipeline.
Regex is definitely the way to go. Building a DOM is overly complicated and itself requires a lot of text parsing.
If all you are doing is scraping a table into a datafile, regex will be just fine, and may be even better than using a DOM document. DOM documents will use up a lot of memory (especially for really large data tables) so you probably want a SAX parser for large documents.