Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Java - Dom Parser Example

// Simple Parser XML with DOM

package parser;

import java.io.File;
import java.io.IOException;

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

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DOMParser
{
	private Document doc = null;
	
	public DOMParser()
	{
		try
		{
			doc = parserXML(new File("parser/file.xml"));
			
			visit(doc, 0);
		}
		catch(Exception error)
		{
			error.printStackTrace();
		}
	}
	
	public void visit(Node node, int level)
	{
		NodeList nl = node.getChildNodes();
		
		for(int i=0, cnt=nl.getLength(); i<cnt; i++)
		{
			System.out.println("["+nl.item(i)+"]");
			
			visit(nl.item(i), level+1);
		}
	}
	
	public Document parserXML(File file) throws SAXException, IOException, ParserConfigurationException
	{
		return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
	}
	
	public static void main(String[] args)
	{
		new DOMParser();
	}
}

Python - Very Simple Parser

// Very Simple Parser

from sgmllib import SGMLParser

import urllib

class ParserHTML(SGMLParser):

	def scrivi(self):
		self.f = open('/tmp/fileOUT.html', 'w')

	def unknown_starttag(self, tag, attrs):

		value = 0
		startTAG = '<' + tag
		
		for i in attrs:
			if(i[0].lower() == i[1].lower() and not i[0] == i[1]):
				startTAG = startTAG[:-1] + ' ' + str(i[1])
				value = 1
			else:
				startTAG += ' ' + str(i[0]) + '="' + str(i[1]) + '"'
				value = 0
		
		if(value == 1): startTAG += '"'

		startTAG += '>'
		self.f.write(startTAG + "\n")

	def handle_data(self, data):

		self.f.write(data + "\n")

	def unknown_endtag(self, tag):

		self.f.write('</' + tag + '>' + "\n")

if __name__ == '__main__':

	p = ParserHTML()
	p.scrivi()
	p.feed(open('/tmp/fileIN.html', 'r').read())

Java - imgFromJar

// Inserire un immagine presente nel file jar dell'applicazione

try
		{
			icon = ImageIO.read(getClass().getResourceAsStream("imgs/icon.png")); // La preleva dal file jar
			setIconImage(icon);
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}

Python - getFile Internet

// Scaricare un file dalla rete

package get.file.example;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class GetFileExample
{
	public GetFileExample()
	{
		try
		{
			byte[] bite = new byte[2048];
			int read;
			URL url = new URL("http://switch.dl.sourceforge.net/sourceforge/pys60miniapps/FlickrS60_src_v0.1b.tar.bz2");
			
			BufferedInputStream bis = new BufferedInputStream(url.openStream());
			FileOutputStream fos = new FileOutputStream("/tmp/file.tar.bz2");
			
			while((read = bis.read(bite))>0)
			{
				fos.write(bite, 0, read);
				System.out.println("--");
			}
			
			fos.close();
			bis.close();
			
			System.out.println("FINE");
			
		}
		catch(MalformedURLException e)
		{
			e.printStackTrace();
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args)
	{
		new GetFileExample();
	}
}
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS