从xml的字段列表中获取对象

Get object from field list of xml

提问人:Nikita Zakharov 提问时间:2/13/2023 更新时间:2/13/2023 访问量:60

问:

请提出最佳解决方案。 有以下 xml 文件

<?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>

还有 5 个类:目录、CD、专辑、艺术家、国家/地区、注册表

如何获取带有填充字段的注册表对象:

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>
}

  • public 类 Catalog {
  • @JacksonXmlProperty(localName = "CD")
    
  • @JacksonXmlElementWrapper(useWrapping = false)
    
  • 私有列表 cds;
  • public list getCds() {
  • 归还CD;
  • }
    
  • }
  • 公共类 Cd {
  • @JsonProperty("TITLE") // Album name
    
  • private String title;
  • @JsonProperty("ARTIST") // Artist name
    
  • 私人弦乐艺术家;
  • @JsonProperty("COUNTRY") // Country name
    
  • 私人弦乐国家;
  • @JsonProperty("COMPANY")
    
  • 私人弦乐公司;
  • @JsonProperty("PRICE")
    
  • 私人双倍价格;
  • @JsonProperty("YEAR")  // Album year
    
  • 私人国际年;
  • }
Java XML Jackson XML 解析 嵌套列表

评论

0赞 Community 2/13/2023
请澄清您的具体问题或提供其他详细信息,以准确强调您的需求。正如目前所写的那样,很难确切地说出你在问什么。

答:

0赞 jdweng 2/13/2023 #1

使用 Powershell。下面将 xml 输入到表格中。您可以使用 Group-Object 或 Sort-Object。NotePropertyName 是一个哈希。

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