<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Forum Pasja Informatyki - Najnowsze z tagiem #xstream</title>
<link>https://forum.pasja-informatyki.pl/tag/%23xstream</link>
<description>Powered by Question2Answer</description>
<item>
<title>biblioteka xstream</title>
<link>https://forum.pasja-informatyki.pl/259522/biblioteka-xstream</link>
<description>

&lt;p&gt;Witam, mam plik xml ktory sam wygenerowalem swoim programem, lecz mam problem z odczytaniem tego pliku w programie do konwersji z xml do javy. Zamieszczam klase Artist, plik xml oraz main.&lt;/p&gt;



&lt;pre class=&quot;brush:java;&quot;&gt;
package przyklad;

import java.util.ArrayList;


public class Artist {
	public String name;
    public boolean isAlive;
    public String url;
    public ArrayList&amp;lt;Album&amp;gt; albums = new ArrayList&amp;lt;Album&amp;gt;();
 
    public Artist(String name, boolean isAlive, String url) {
        this.name = name;
        this.isAlive = isAlive;
        this.url = url;
    }
 
    public void addAlbum(Album album) {
        albums.add(album);
    }
 
}


class Album {
   public String title;
   public int noOfrecords;
   public int year;

   public Album(String title, int noOfrecords, int year) {
       this.title = title;
       this.noOfrecords = noOfrecords;
       this.year = year;
   }
}&lt;/pre&gt;



&lt;pre class=&quot;brush:java;&quot;&gt;
package przyklad;
 
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class przyklad{
	
	public static void main(String[] args) {
		String filename = &quot;plik.xml&quot;;
		ArrayList&amp;lt;Artist&amp;gt; artysci = new ArrayList&amp;lt;Artist&amp;gt;();
		artysci = xml2ArrayListArtist(filename);
		System.out.println(artysci);
	}
	
	public static ArrayList&amp;lt;Artist&amp;gt; xml2ArrayListArtist(String filename) {
		String xml = &quot;&quot;;
		String strLine = &quot;&quot;;
		if (filename != null) {
			try {
				FileInputStream f = new FileInputStream(filename);
				DataInputStream in = new DataInputStream(f);
				BufferedReader r = new BufferedReader(new InputStreamReader(in));
				while ((strLine = r.readLine()) != null)
					xml += strLine;
				in.close();
				XStream mapping = new XStream(new DomDriver());
				return (ArrayList&amp;lt;Artist&amp;gt;) mapping.fromXML(xml);
			} catch (Exception e) {
				System.out.println(e);
				System.exit(0);
			}
		}
		return null;
	}
}
&lt;/pre&gt;



&lt;pre class=&quot;brush:xml;&quot;&gt;
&amp;lt;list&amp;gt;
- &amp;lt;przyklad2.Artist&amp;gt;
  &amp;lt;name&amp;gt;wiesiek&amp;lt;/name&amp;gt; 
  &amp;lt;isAlive&amp;gt;false&amp;lt;/isAlive&amp;gt; 
- &amp;lt;albums&amp;gt;
- &amp;lt;przyklad2.Album&amp;gt;
  &amp;lt;title&amp;gt;Bix Beiderbecke with the Paul Whiteman Orchestra&amp;lt;/title&amp;gt; 
  &amp;lt;noOfrecords&amp;gt;5&amp;lt;/noOfrecords&amp;gt; 
  &amp;lt;year&amp;gt;1928&amp;lt;/year&amp;gt; 
  &amp;lt;/przyklad2.Album&amp;gt;
- &amp;lt;przyklad2.Album&amp;gt;
  &amp;lt;title&amp;gt;Bix Beiderbecke and His Gang&amp;lt;/title&amp;gt; 
  &amp;lt;noOfrecords&amp;gt;6&amp;lt;/noOfrecords&amp;gt; 
  &amp;lt;year&amp;gt;1927&amp;lt;/year&amp;gt; 
  &amp;lt;/przyklad2.Album&amp;gt;
  &amp;lt;/albums&amp;gt;
  &amp;lt;/przyklad2.Artist&amp;gt;
  &amp;lt;/list&amp;gt;&lt;/pre&gt;



&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description>
<category>Java</category>
<guid isPermaLink="true">https://forum.pasja-informatyki.pl/259522/biblioteka-xstream</guid>
<pubDate>Sun, 04 Jun 2017 18:33:20 +0000</pubDate>
</item>
</channel>
</rss>