0

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

  • public class Catalog {
  • @JacksonXmlProperty(localName = "CD")
    
  • @JacksonXmlElementWrapper(useWrapping = false)
    
  • private List cds;
  • public List getCds() {
  • return cds;
  • }
    
  • }
  • public class Cd {
  • @JsonProperty("TITLE") // Album name
    
  • private String title;
  • @JsonProperty("ARTIST") // Artist name
    
  • private String artist;
  • @JsonProperty("COUNTRY") // Country name
    
  • private String country;
  • @JsonProperty("COMPANY")
    
  • private String company;
  • @JsonProperty("PRICE")
    
  • private double price;
  • @JsonProperty("YEAR")  // Album year
    
  • private int year;
  • }
1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Feb 12, 2023 at 20:01

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.