Gerando um XML com JDOM

3 respostas
M

Ola amigos,
Estou tentando gerar um XML utilizando JDOM.
peguei alguns exemplos, mas não consigo fazer funcionar.

segue uma parte do código exemplo:

Element carElement = new Element("car");
Document myDocument = new Document(carElement);

carElement.addAttribute(new Attribute("vin", "123fhg5869705iop90"));

Element make = new Element("make");
make.addContent("Toyota");
carElement.addContent(make);

O Eclipse marca como erro o “.addAttribute” da linha: carElement.addAttribute(new Attribute(“vin”, “123fhg5869705iop90”));

A descrição do erro é:
Multiple markers at this line
- Syntaxe error on token, misplaced construct(s)
- Syntaxe error on token “addAttribute”, = expected after this token

e també, marcar como erro o “.” e o "“Toyota” da linha: make.addContent(“Toyota”);

A descrição do erro é:
Multiple markers at this line
- Syntaxe error on token, misplaced construct(s)
- Syntaxe error on token ““Toyota””, delete this token

Estou utilizando os seguites import:

import org.jdom.Attribute;

import org.jdom.Comment;

import org.jdom.Document;

import org.jdom.Element;

import org.jdom.JDOMException;

import org.jdom.input.SAXBuilder;

import org.jdom.output.XMLOutputter;
  • Acho que estou cometento algum erro básico, mas como sou iniciante no java, não consegui identificar o q é.

Grato pela ajuda

Marcos Batalha

3 Respostas

filipefox

Amigo, a classe Element não possui o metodo addAttribute. Ela tem o setAttribute(Attribute).

carElement.setAttribute(new Attribute("vin", "123fhg5869705iop90"));

Tenta mudar o nome do metodo e ver se faz o que vc quer. Abs!

M

O erro permanece o mesmo.

Já havia tentato o método setAttribute…

faelcavalcanti

recomendo usar o Stax bem mais rápido para geração

Criado 5 de setembro de 2008
Ultima resposta 8 de set. de 2008
Respostas 3
Participantes 3