domingo, 28 de outubro de 2007

Problema com chave no JS

Unica diferença nos dois objetos é a posição da chave depois do return


var newSchool = function(){
return{
getName: function(variableString){
if(variableString == 'Chave depois do return'){
return 'OK, Teste Funcionando';
} else {
return 'Fail'
}
}
};
}();

var newSchool2 = function(){
return
{
getName2: function(variableString){
if(variableString == 'Chave abaixo do return'){
return 'Teste Bugado, apenas pela posição da chave depois do return';
} else {
return 'Fail'
}
}
};
}();



href="#" onclick="alert(newSchool.getName('Chave depois do return'));">
Teste Funcionando

href="#" onclick="alert(newSchool2.getName2('Chave abaixo do return'));">
Teste Bugado

Como rodar sem Main?

public class NoMain
{
static
{
System.out.println("Executando sem metodo main");
System.exit(0);
}
}

Agora é só testar:
javac NoMain.java
java NoMain

quarta-feira, 24 de outubro de 2007

Como criar e ler um XML

Tem que baixar o .jar http://xstream.codehaus.org

public class Exportar
{

@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception
{
Aluno aluno1 = new Aluno(1L, "Joao da Penha", new Date());
Aluno aluno2 = new Aluno(2L, "Maria Joao", new Date());

Collection alunos = new ArrayList();
alunos.add(aluno1);
alunos.add(aluno2);

//Criando um xml
String encoding = "ISO-8859-1";
XStream stream = new XStream(new DomDriver(encoding));
stream.alias("aluno", Aluno.class);

File xmlFile = new File("C:\\aluno.xml");

String xmlFileContent = new String("\r\n".getBytes(), encoding);
xmlFileContent += stream.toXML(alunos);

System.out.println("########### PRONTO TA CRIADO O XML #############");
System.out.println(xmlFileContent);

FileWriter fileWriter = new FileWriter(xmlFile);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(xmlFileContent);

bufferedWriter.flush();
bufferedWriter.close();

//Lendo um xml
Collection alunosInput = new ArrayList();

BufferedReader inputXml = new BufferedReader(new FileReader("C:\\aluno.xml"));
alunosInput = (Collection) stream.fromXML(inputXml);
inputXml.close();


System.out.println("########### PRONTO TA CRIADO O OBJETO #############");

for (Aluno aluno : alunosInput)
{
System.out.println(aluno.getId());
System.out.println(aluno.getNome());
System.out.println(aluno.getNascimento());
}
}
}

Aluno.class

public class Aluno
{
public Aluno()
{
super();
}

public Aluno(Long id, String nome, Date nascimento)
{
super();
this.id = id;
this.nome = nome;
this.nascimento = nascimento;
}

private Long id;
private String nome;
private Date nascimento;

public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getNascimento() {
return nascimento;
}
public void setNascimento(Date nascimento) {
this.nascimento = nascimento;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}