Nikita Zakharov
Nikita Zakharov

Reputation: 31

Get object from field list of xml

Please suggest the best solution. There is the following xml file

<?xml version="1.0" encoding="UTF-8"?>
<CATALOG>
    <CD>
        <TITLE>Empire Burlesque</TITLE>
        <ARTIST>Bob Dylan</ARTIST>
        <COUNTRY>USA</COUNTRY>
        <COMPANY>Columbia</COMPANY>
        <PRICE>10.90</PRICE>
        <YEAR>1985</YEAR>
    </CD>
    <CD>
        <TITLE>Hide your heart</TITLE>
        <ARTIST>Bonnie Tyler</ARTIST>
        <COUNTRY>UK</COUNTRY>
        <COMPANY>CBS Records</COMPANY>
        <PRICE>9.90</PRICE>
        <YEAR>1988</YEAR>
    </CD>
    <CD>
        <TITLE>Greatest Hits</TITLE>
        <ARTIST>Dolly Parton</ARTIST>
        <COUNTRY>USA</COUNTRY>
        <COMPANY>RCA</COMPANY>
        <PRICE>9.90</PRICE>
        <YEAR>1982</YEAR>
    </CD>
    <CD>
        <TITLE>Still got the blues</TITLE>
        <ARTIST>Gary Moore</ARTIST>
        <COUNTRY>UK</COUNTRY>
        <COMPANY>Virgin records</COMPANY>
        <PRICE>10.20</PRICE>
        <YEAR>1990</YEAR>
    </CD>
    <CD>
        <TITLE>Eros</TITLE>
        <ARTIST>Eros Ramazzotti</ARTIST>
        <COUNTRY>EU</COUNTRY>
        <COMPANY>BMG</COMPANY>
        <PRICE>9.90</PRICE>
        <YEAR>1997</YEAR>
    </CD>
    <CD>
        <TITLE>One night only</TITLE>
        <ARTIST>Bee Gees</ARTIST>
        <COUNTRY>UK</COUNTRY>
        <COMPANY>Polydor</COMPANY>
        <PRICE>10.90</PRICE>
        <YEAR>1998</YEAR>
    </CD>
</CATALOG>

also have 5 classes: Catalog, Cd, Album, Artist, Country, Registry

How can i get object of Registry with filled fields:

public class Registry {

    private List<Country> countries = new ArrayList<>();
}

public class Country {
    private String name; // <COUNTRY>...</COUNTRY>
    private List<Artist> artists = new ArrayList<>();
}

public class Artist {
    private String name; // <ARTIST>...</ARTIST>
    private List<Album> albums = new ArrayList<>();
}

public class Album {
    private String name; // <TITLE>...</TITLE>
    private int year; // <YEAR>...</YEAR>
}

And

Upvotes: 0

Views: 99

Answers (1)

jdweng
jdweng

Reputation: 34431

Use Powershell. Below will input xml in to table. Than you can use Group-Object or Sort-Object. The NotePropertyName is a hash.

using assembly System.Xml.Linq

$Filename = "c:\temp\test.xml"

$albums = [System.Collections.ArrayList]::new()  

$xDoc = [System.Xml.Linq.XDocument]::Load($Filename)
foreach($cd in $xDoc.Descendants("CD"))
{
   $newAlbum = New-Object -TypeName psobject
   
   $title = $cd.Element("TITLE").Value
   $newAlbum | Add-Member -NotePropertyName Title -NotePropertyValue $title

   $artist = $cd.Element("ARTIST").Value
   $newAlbum | Add-Member -NotePropertyName Artist -NotePropertyValue $artist   
   
   $country = $cd.Element("COUNTRY").Value
   $newAlbum | Add-Member -NotePropertyName Country -NotePropertyValue $country   
   
   $company = $cd.Element("COMPANY").Value
   $newAlbum | Add-Member -NotePropertyName Company -NotePropertyValue $company   
   
   $price = [decimal]$cd.Element("PRICE").Value
   $newAlbum | Add-Member -NotePropertyName Price -NotePropertyValue $price
     
   $year = [int]$cd.Element("YEAR").Value
   $newAlbum | Add-Member -NotePropertyName Year -NotePropertyValue $year


   $albums.Add($newAlbum) | Out-Null
}
Write-Host "All Albums"
$albums | Format-Table

Upvotes: 0

Related Questions