Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Análise de um arquivo XML usando JSR 172

From Wiki
Jump to: navigation, search
Article Metadata

Compatibilidade
Plataforma(s):
Series 40
Symbian

Artigo
Tradução:
Por valderind4
Última alteração feita por lpvalente em 25 Jan 2014

A intenção deste trecho de código e prover uma idéia sobre como analisar um arquivo XML usando a JSR 172.

  • Este exemplo recebe uma URL de um arquivo XML
  • Abre uma HttpConnection para o arquivo pegar o InputStream
  • Usa o Input Stream como um meio para criar um SAXParser

Quando o SAXParser é criado com sucesso, a aplicação usará o retorno para controlar todo o documento.

import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
 
/**
*Class in Charge of http connectivity and xml parsing
*/

public class XMLParser extends DefaultHandler implements Runnable {
 
private String URL;
private HttpConnection http;
StringBuffer log = new StringBuffer();
 
/**
* Starts connecting to the given URL to fetch the XML file.
* The connection is started in a separate thread
* @param URL
*/

public void connect(String URL) {
this.URL = URL;
Thread thread = new Thread(this);
thread.start();
}
 
/**
* Opens the connection and fetches the xml file
*/

public void run() {
loadXML(getInputStream());
}
 
private InputStream getInputStream() {
InputStream input = null;
try {
http = (HttpConnection) Connector.open(URL);
 
// Getting the response code will open the connection,
int rc = http.getResponseCode();
if (rc == HttpConnection.HTTP_OK) {
String type = http.getType();
if (type.startsWith("text/xml")) {
 
input = http.openInputStream();
}
}
} catch (IOException ex) {
}
return input;
}
 
/**
* Starts parsing the received XML file using a SAXParser
*/

public void loadXML(InputStream input) {
 
SAXParser parser;
try {
SAXParserFactory factory =
SAXParserFactory.newInstance();
parser =
factory.newSAXParser();
 
InputSource fis = new InputSource(input);
parser.parse(fis, this);
 
} catch (Exception e) {
e.printStackTrace();
}
 
}
 
/**
* Callback invoked when a start tag is loaded
* @param uri
* @param localName
* @param qName
* @param attributes
* @throws org.xml.sax.SAXException
*/

public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
}
 
/**
* Callback invoked when an end tag is found
* @param uri
* @param localName
* @param qName Nombre de la etiqueta
* @throws org.xml.sax.SAXException
*/

public void endElement(String uri, String localName, String qName)
throws SAXException {
}
 
/**
* Callback invoked when the tag contains a text.
* Text is saved according to the tag being analyzed
* as the callback does not pass any reference to the current tag
* a global variable currentTag is used to store the value.
* Data is saved in the current visit
* @param ch
* @param start
* @param length
* @throws org.xml.sax.SAXException
*/

public void characters(char[] ch, int start, int length) throws SAXException {
String chars = new String(ch, start, length).trim();
if (chars.length() > 0) {
}
}
}
This page was last modified on 25 January 2014, at 18:17.
200 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×