Ler xml com xstream

7 respostas
william_sistema

tenho o seguinte xml

conecta.xml

<conecta> <textoconecta>jdbc:mysql://localhost/syndic?user=root&password=123456</textoconecta> </conecta>

gostaria de ler o conteúdo do textoconecta e passar para uma variavel String.

lembrando que o xml estará na pasta raiz da aplicação (seu eu não me engano (pach))

como fazer isso usando XStream.

grato pela atenção

7 Respostas

nextuser

[size=24]http://xstream.codehaus.org/tutorial.html
[/size]

Two Minute Tutorial

This is a very quick introduction to XStream. Skim read it to get an idea of how simple it is to convert objects to XML and back again. I’m sure you’ll have questions afterwards.
Create classes to be serialized

Here’s a couple of simple classes. XStream can convert instances of these to XML and back again.

public class Person {

private String firstname;

private String lastname;

private PhoneNumber phone;

private PhoneNumber fax;

// … constructors and methods

}
public class PhoneNumber {

private int code;

private String number;

// … constructors and methods

}

Note: Notice that the fields are private. XStream doesn’t care about the visibility of the fields. No getters or setters are needed. Also, XStream does not limit you to having a default constructor.
Initializing XStream

To use XStream, simply instantiate the XStream class:

XStream xstream = new XStream();

You require xstream-[version].jar and xpp3-[version].jar in the classpath. XPP3 is a very fast XML pull-parser implementation. If you do not want to include this dependency, you can use a standard JAXP DOM parser instead:

XStream xstream = new XStream(new DomDriver()); // does not require XPP3 library

Note: This class is a simple facade designed for common operations. For more flexibility you may choose to create your own facade that behaves differently.

Now, to make the XML outputted by XStream more concise, you can create aliases for your custom class names to XML element names. This is the only type of mapping required to use XStream and even this is optional.

xstream.alias(“person”, Person.class);
xstream.alias(“phonenumber”, PhoneNumber.class);

Note: This is an optional step. Without it XStream would work fine, but the XML element names would contain the fully qualified name of each class (including package) which would bulk up the XML a bit. See the Alias Tutorial a more complete introduction.
Serializing an object to XML

Let’s create an instance of Person and populate its fields:

Person joe = new Person(Joe, Walnes);

joe.setPhone(new PhoneNumber(123, 1234-456));

joe.setFax(new PhoneNumber(123, 9999-999));

Now, to convert it to XML, all you have to do is make a simple call to XStream:

String xml = xstream.toXML(joe);

The resulting XML looks like this:

Joe Walnes 123 1234-456 123 9999-999

It’s that simple. Look at how clean the XML is.
Deserializing an object back from XML

To reconstruct an object, purely from the XML:

Person newJoe = (Person)xstream.fromXML(xml);

And that’s how simple XStream is!
Summary

To recap:

* Create element name to class name aliases for any custom classes using xstream.alias(String elementName, Class cls);
* Convert an object to XML using xstream.toXML(Object obj);
* Convert XML back to an object using xstream.fromXML(String xml);
william_sistema

Caro.

ate ja dei uma olhada ai porém não consegui compreender completamente
gostaria de algo assim.

public String conec;

conec ==XLM.textoconecta; >>>>> ISSO NÃO EXITE>>>>>

é algo assim que quero, só pegar o valor do xml, aquele exemplo parece que apenas gera o xml .
no meu caso eu já tenho o xml.
se puder me ajudar ou mostrar a linha de onte está.

mai uma vez grato.

nextuser

cara nuam sei se em delfii isso existe mas em java para ser algo mais robusto é tb mais complexo… ou vc usa o XS ou usa SAX ou DOM…

unico lugar que vi isso é em actionscript!! que é bizuu :smiley:

william_sistema

Entendi,

mais o exemplo que vc me passou tb le o xml ou apenas gera um xml?

gostaria apenas de ler o xml que coloquei no começo.

nextuser

vc nem se deu o trabalho de ler né??? no final ta la:

Person newJoe = (Person)xstream.fromXML(xml);

se newJoe é um objeto java e o metodo fromXML pega do xml e se o paramentro xml é o XML…

bom acho que isso quer dizer que ele popula um objeto java com os valores do XML…

legal né?

william_sistema

pior que li, só que ainda não sou muito bom em java, e tem muita coisa que não entendo.

estou aprendendo, por isso ainda sofro bastante.

desculpa a falta de esperiencia e obrigado pela ajuda.

leandroToledo

putz q vontade do cara ajudar…

:wink:

Criado 9 de março de 2010
Ultima resposta 21 de abr. de 2010
Respostas 7
Participantes 3