alternative to xpath ?

Put your problem here if it does not fit any of the other categories.

alternative to xpath ?

Postby JesseJames » Tue Apr 12, 2011 6:01 pm

Hello,
I need to access the "Description" node of my XML document:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Level>
  3.     <Description>This is what I need to access</Description>
  4.     <!-- a lot of other data follow -->
  5. </Level>
  6.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


Using xpath it's pretty simple:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. InputStream is = getAssets().open(levelFile);
  2. DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
  3. builderFactory.setNamespaceAware(true);
  4. DocumentBuilder builder = builderFactory.newDocumentBuilder();
  5. Document document = builder.parse(is);
  6. XPath xpath = XPathFactory.newInstance().newXPath();
  7. String levelDescription = (String) xpath.evaluate("/Level/Description", document, XPathConstants.STRING);
  8.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


The problem is that javax.xml.xpath.XPath was introduces only in Android 2.2 and I don't want to break the backward compatibility of my app just because of this.

I could parse the file using SAXParser :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     public String loadLevelDescriptionFromStream(final InputStream pInputStream) throws IOException {
  2.         String  description = "";
  3.         try{
  4.             final SAXParserFactory spf = SAXParserFactory.newInstance();
  5.             final SAXParser sp = spf.newSAXParser();
  6.  
  7.             final XMLReader xr = sp.getXMLReader();
  8.  
  9.             LevelDescriptionHandler handler = new LevelDescriptionHandler();
  10.             xr.setContentHandler(handler);
  11.             xr.parse(new InputSource(new BufferedInputStream(pInputStream)));
  12.             description = handler.getDescription();
  13.            
  14.         } catch (final SAXException se) {
  15.             Debug.e(se);
  16.             /* Doesn't happen. */
  17.         } catch (final ParserConfigurationException pe) {
  18.             Debug.e(pe);
  19.             /* Doesn't happen. */
  20.         } finally {
  21.             StreamUtils.closeStream(pInputStream);
  22.         }
  23.        
  24.         return description;
  25.     }
  26.    
  27.     private class LevelDescriptionHandler extends DefaultHandler {
  28.         boolean inDescription = false;
  29.         String description = "";
  30.        
  31.         @Override
  32.         public void startElement(String uri, String localName,
  33.                         String qName, Attributes attributes)
  34.                         throws SAXException {
  35.                 if(localName.equalsIgnoreCase("Description") {
  36.                         inDescription = true;
  37.                 } else {
  38.                         inDescription = false;
  39.                 }
  40.         }
  41.        
  42.         @Override
  43.         public void characters(char[] ch, int start, int length)
  44.                         throws SAXException {
  45.                 if(inDescription) {
  46.                         description += new String(ch, start, length);
  47.                 }
  48.         }
  49.        
  50.         public String getDescription() {
  51.                 return description;
  52.         }
  53.        
  54.     }
  55.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4



but it's a kind of complex just for accessing one node element. Is there an alternative to SAXParser ?

cheers,
Jesse
JesseJames
Freshman
Freshman
 
Posts: 8
Joined: Sat Dec 04, 2010 3:13 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 19 guests