Getting started with XMLPullParser - xmlpullparser

I am trying to use XMLPullParser but I cannot find any useful tutorials. Based off of the instructions on http://xmlpull.org/ I need to download an implementation of XMLPullParser as a jar file and then add it to my class path. However I cannot find any link to any jar file that works. Does anyone know where I might be able to find a jar file I can download.
Thanks

Ok, here it is for you.
From the official doc :
XmlPull API Implementations:
XNI 2 XmlPull
XPP3/MXP1
KXML2
Here i use KXML2.
Steps :
Download KXML2 jar file from here.
Create a new java project
Create a new class
Right click the java project -> Properties -> Java Build path -> Libraries -> Add external jar's -> Add downloaded kxml2 jar file.
Java code
import java.io.IOException;
import java.io.StringReader;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
public class XmlPullparserBasic {
public static void main (String args[]) throws XmlPullParserException, IOException
{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput( new StringReader ( "<foo>Hello World!</foo>" ) );
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_DOCUMENT) {
System.out.println("Start document");
} else if(eventType == XmlPullParser.START_TAG) {
System.out.println("Start tag "+xpp.getName());
} else if(eventType == XmlPullParser.END_TAG) {
System.out.println("End tag "+xpp.getName());
} else if(eventType == XmlPullParser.TEXT) {
System.out.println("Text "+xpp.getText());
}
eventType = xpp.next();
}
System.out.println("End document");
}
}
Output :
Hope it helps!

Related

Eclipse LTK refactoring - Resources path not being updated when project is renamed

I am writing custom refactoring for renaming project/folder/file in Eclipse RCP application. I have extended the Refactoring class and wrote my own ResourceChange class where I am updating the project resources by calling IProject.move() method.
The rename is working fine but path of the resources under the project are not updated.
Any pointers will be helpful.
Thanks.
Update:
Refactoring class Code:
#Override
public Change createChange(IProgressMonitor pm) {
pm.beginTask("Renaming", 1);
CompositeChange change = new CompositeChange("Renaming");
if(resource instanceof IProject) {
return new CustomResourceChange((IProject)resource, resourceName);
}
pm.done();
return change;
}
CustomResourceChange class code:
protected IPath createNewPath() {
return modifiedResourcePath.removeLastSegments(1).append(resourceNewName);
}
#Override
public Change perform(IProgressMonitor pm) throws CoreException {
try {
pm.beginTask("Renaming Project", 1);
Change change = new CustomResourceChange(modifiedResource, createNewPath(), resourceOldName, resourceNewName);
IProject project = (IProject)ResourcesPlugin.getWorkspace().getRoot().findMember(modifiedResourcePath);
if (project != null) {
IProjectDescription description = project.getDescription();
description.setName(resourceNewName);
project.move(description, IResource.FORCE, new SubProgressMonitor(pm, 1));
}
return change;
} finally {
pm.done();
}
}

How can I obtain the list of files in a project from VSIX(/MPF) code?

I am building a VSIX package to support a custom language in Visual Studio using MPF. I am in a custom designer and I need to find the files referenced in the project to resolve some dependencies. Where can I access this list?
I assume, that you´re using MPF to implement the project system for your custom language service. When doing so, you probably have a project root node which is derived from either ProjectNode or HierarchyNode...
If so, you could share the root node´s instance with the designer and try to find files by traversing the hierarchy, for instance...
internal class HierarchyVisitor
{
private readonly Func<HierarchyNode, bool> filterCallback;
public HierarchyVisitor(
Func<HierarchyNode, bool> filter)
{
this.filterCallback = filter;
}
public IEnumerable<HierarchyNode> Visit(
HierarchyNode node)
{
var stack = new Stack<HierarchyNode>();
stack.Push(node);
while (stack.Any())
{
HierarchyNode next = stack.Pop();
if (this.filterCallback(next))
{
yield return next;
}
for (
HierarchyNode child = next.FirstChild;
child != null;
child = child.NextSibling)
{
stack.Push(child);
}
}
}
}
To get a list of all nodes in the hierarchy, you could just do...
ProjectNode root = ...
var visitor = new HierarchyVisitor(x => true);
IEnumerable<HierarchyNode> flatList = visitor.Visit(root);
Or to filter for a certain file type, you could try something like this...
ProjectNode root = ...
var visitor = new HierarchyVisitor((HierarchyNode x) =>
{
const string XmlFileExtension = ".xml";
string path = new Uri(x.Url, UriKind.Absolut).LocalPath;
return string.Compare(
XmlFileExtension,
Path.GetFileExtension(path),
StringComparison.InvariantCultureIgnoreCase) == 0;
});
IEnumerable<HierarchyNode> xmlFiles = visitor.Visit(root);

eventhough i change the environmental variables and classpath, while running sqlite jdbc program i get the error stating no class definition doun

im running a jdbc program on Sqlite. though i change the environmental variables or define the classpath of the jar file sqlite-jdbc-3.7.2.jar, i get an error stating ClassNotFoundException: org.sqlite.JDBC... how to rectify it?
my code is`
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Sample
{
public static void main(String[] args)throws ClassNotFoundException
{
// load the sqlite-JDBC driver using the current class loader
Class.forName("org.sqlite.JDBC");
Connection connection = null;
try
{
// create a database connection
connection = DriverManager.getConnection("jdbc:sqlite:sample.db");
Statement statement = connection.createStatement();
statement.setQueryTimeout(30); // set timeout to 30 sec.
statement.executeUpdate("drop table if exists person");
statement.executeUpdate("create table person (id integer, name string)");
statement.executeUpdate("insert into person values(1, 'leo')");
statement.executeUpdate("insert into person values(2, 'yui')");
ResultSet rs = statement.executeQuery("select * from person");
while(rs.next())
{
// read the result set
System.out.println("name = " + rs.getString("name"));
System.out.println("id = " + rs.getInt("id"));
}
}
catch(SQLException e)
{
// if the error message is "out of memory",
// it probably means no database file is found
System.err.println(e.getMessage());
}
finally
{
try
{
if(connection != null)
connection.close();
}
catch(SQLException e)
{
// connection close failed.
System.err.println(e);
}
}
}
}`
my jar file is sqlite-jdbc-3.7.2.jar
my class path is D:\jdk1.6.0_45\sqlite-jdbc-3.7.2.jar
my environmental variable is also the same
what should i do?
help pls
i found the solution to my problem...
the class path should be like
`javac Sample.java
java -classpath "D:\jdk1.6.0_45\sqlite-jdbc-3.7.2.jar";. Sample`
the problem is solved:)

Byte Code off-card verifier for cap file

I need to verify a Java Card programme (a cap file) using an off-card byte code verifier.
I have manually modify the informations in the cap file and i want to verify if the new cap file is well type.
I try to use the com.sun.javacard.scriptgen.CAP.verifyCAP() methode. I send valid and invalid cap files but the result is always equal to 0.
I never post the solution. That day has finally arrived ;)
Download the JavaCard SDK. It contain a compiled version of the verifier. For that exemple that will be java_card_kit-2_2_1.
My files:
./
./java_card_kit-2_2_1/
./java2CAP.sh
./ValidApplet/
./ValidApplet/ValidApp.java
./verifyCAP.sh
java2CAP.sh:
#!/bin/bash
export JC_HOME=./java_card_kit-2_2_1
export CLASSPATH=$JC_HOME/lib/apduio.jar:$JC_HOME/lib/apdutool.jar:$JC_HOME/lib/jcwde.jar:$JC_HOME/lib/converter.jar:$JC_HOME/lib/scriptgen.jar:$JC_HOME/lib/offcardverifier.jar:$JC_HOME/lib/api.jar:$JC_HOME/lib/capdump.jar:$JC_HOME/lib/:$JC_HOME/samples/classes:$CLASSPATH
PACKAGE=ValidApplet
CLASS=ValidApp
APPLET=$CLASS.java
PACKAGE_AID=0x46:0x56:0x55:0x4c:0x4e:0x54:0x45:0x53:0x54:0x53
APPLET_AID=0x46:0x56:0x55:0x4c:0x4e:0x54:0x45:0x53:0x54:0x53:0x41:0x70:0x70
javac -g -source 1.3 -target 1.1 $PACKAGE/$APPLET
java com.sun.javacard.converter.Converter -nobanner -out CAP -exportpath $JC_HOME/api_export_files -applet $APPLET_AID $CLASS $PACKAGE $PACKAGE_AID 1.0 -i
verifyCAP.sh:
#!/bin/bash
export JC_HOME=./java_card_kit-2_2_1
export CLASSPATH=$JC_HOME/lib/apduio.jar:$JC_HOME/lib/apdutool.jar:$JC_HOME/lib/jcwde.jar:$JC_HOME/lib/converter.jar:$JC_HOME/lib/scriptgen.jar:$JC_HOME/lib/offcardverifier.jar:$JC_HOME/lib/api.jar:$JC_HOME/lib/capdump.jar:$JC_HOME/lib/:$JC_HOME/samples/classes:$CLASSPATH
export CAPP_PATH=./ValidApplet/javacard
export CAPP_NAME=ValidApplet.cap
java -classpath $JC_HOME/lib/offcardverifier.jar com.sun.javacard.offcardverifier.Verifier $JC_HOME/api_export_files/javacard/framework/javacard/framework.exp $JC_HOME/api_export_files/java/lang/javacard/lang.exp $JC_HOME/api_export_files/javacard/security/javacard/security.exp $CAPP_PATH/$CAPP_NAME
./ValidApplet/ValidApp.java:
package ValidApplet;
import javacard.framework.Applet;
import javacard.framework.APDU;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
public class ValidApp extends Applet //implements PIN
{
final static byte TEST_CLA = (byte)0x77;
public static class TestClassStatic {};
protected ValidApp()
{
register();
}
public static void install(byte[] bArray, short bOffset, byte bLength)
{
new ValidApp();
}
public void process(APDU apdu)
{
byte buffer[] = apdu.getBuffer();
try {
if (buffer[ISO7816.OFFSET_CLA] == TEST_CLA) {
test((byte)4, (short)2);
} else {
ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
}
} catch (ISOException e) {
}
}
}

How to get folder size in Adobe Air?

How to get folder size in Adobe Air?
Should be fairly simple using File.size. Just in case is confusing, folders in AIR are represented using the File class, which extends FileReference, thus the link to the FileReference documentation.
Recursive folder listings and contents processing
http://cookbooks.adobe.com/post_Recursive_folder_listings_and_contents_processing-9410.html
...has sufficient sample code in it to get you started.
my implementation is:
public static function getFileSize(file:File):Number{
var result:Number = 0;
if(file == null || file.exists == false) {
return 0;
}
if(file.isDirectory){
var files:Array = file.getDirectoryListing();
for each (var f:File in files) {
if(f.isDirectory){
result += getFileSize(f);
}else{
result += f.size;
}
}
}else{
return file.size;
}
return result;
}

Resources