Xml Serializer Uri : Xml « Development « Android






Xml Serializer Uri

     
/**
 *
 */
//package org.alldroid.forum.xml;


import java.net.URL;
import java.util.LinkedList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import android.net.Uri;
import android.util.Log;

/**
 * @author trr4rac
 *
 */
public class XmlSerializer {
  private static final String TAG = XmlSerializer.class.getSimpleName ( );

  private DocumentBuilder builder;
  private Document document;
  /**
   *
   */
  public XmlSerializer ( Uri url ) {
    try {
      builder = DocumentBuilderFactory.newInstance ( ).newDocumentBuilder ( );
      document = builder.parse(new URL(url.toString ( )).openStream ( ));
    } catch ( Exception e ) {
      Log.e(TAG,e.toString ( ));
    }
  }

  public <T extends IXmlSerializable> T deserialize ( Class<T> cls ) throws SAXException {
    T result;

    if ( document == null ) {
      throw new SAXException("Document not available.");
    }

    Element ele = document.getDocumentElement ( );
    try {
      result = cls.newInstance ( );

    } catch ( Exception e ) {
      Log.e(TAG,e.toString ( ));
      return null;
    }

    result.deserialize ( ele );
    return result;
  }
}
interface IXmlSerializable {
  public abstract void deserialize ( Element ele );
}

   
    
    
    
    
  








Related examples in the same category

1.Using xml resource
2.XML Resource Demo
3.Load style from styles.xml
4.Define PreferenceScreen in xml file
5.Using XML Parser
6.XML-defined adapters can be used to easily create adapters in your own application or to pass adapters to other processes.
7.Xml Parse
8.Get Text Content from Xml Node
9.Escape un escape Xml
10.Get Xml node value with substring
11.Get value from Element
12.Get attribute value
13.Get field from NamedNodeMap
14.get Node Value With Attribute
15.get Character Data From Element
16.update Xml