douzhi3454
2014-12-30 18:36 阅读 49
已采纳

如何解析java属性文件并创建填充每个节值并将其放入aray的项目

I have a catalog.properties file of the form as shown below

catalog.package.1234.asset_name=I am the Galaxians
catalog.package.1234.asset_id=1234
catalog.package.1234.version_minor=75
catalog.package.1234.version_major=1
catalog.package.1234.custom_number=27
catalog.package.1234.custom.0.name=Title
catalog.package.1234.custom.0.value=I am the Galaxians
catalog.package.1234.custom.1.name=Summary_Short
catalog.package.1234.custom.1.value=blah!! blah!! blah.. 2014
catalog.package.1234.custom.2.name=StartDate
catalog.package.1234.custom.2.value=2014-11-23 22:00:00
catalog.package.1234.custom.3.name=EndDate
catalog.package.1234.custom.3.value=2015-01-07 04:00:00
catalog.package.1234.custom.5.name=ReleaseDate
catalog.package.1234.custom.5.value=
catalog.package.1234.custom.6.name=Advisories
catalog.package.1234.custom.6.value=V
catalog.package.1234.custom.7.name=Actors
catalog.package.1234.custom.7.value=Pratt,Chris
catalog.package.1234.custom.8.name=Actors
catalog.package.1234.custom.9.value=Bautista,Dave
catalog.package.1234.custom.10.name=Genre
catalog.package.1234.custom.10.value=Movies/Action
catalog.package.1234.custom.11.name=Genre
catalog.package.1234.custom.11.value=Movies/All Movies
catalog.package.1234.asset_number=3
catalog.package.1234.asset.0.name=I am the Galaxians
catalog.package.1234.asset.0.asset_id=file://x.jpg
catalog.package.1234.asset.0.type=poster
catalog.package.1234.asset.0.version_minor=1
catalog.package.1234.asset.0.version_major=1
catalog.package.1234.asset.0.description=blah!! blah!! blah.. 2014
catalog.package.1234.asset.1.name=BOXO_NG-NG-feature-Ingested-Scrambled
catalog.package.1234.asset.1.asset_id=qwerty1234
catalog.package.1234.asset.1.type=scrambled_video
catalog.package.1234.asset.1.version_minor=2
catalog.package.1234.asset.1.version_major=1
catalog.package.1234.asset.1.description=BOXO_NG-NG-feature-Ingested-Scrambled
catalog.package.1234.asset.1.custom_number=7
catalog.package.1234.asset.1.custom.0.name=Audio_Type
catalog.package.1234.asset.1.custom.0.value=Dolby Digital
catalog.package.1234.asset.1.custom.1.name=HDContent
catalog.package.1234.asset.1.custom.1.value=Y
catalog.package.1234.asset.1.custom.2.name=Dubbed_Languages
catalog.package.1234.asset.1.custom.2.value=
catalog.package.1234.asset.1.ca.product_number=1
catalog.package.1234.asset.1.ca.product.0.id=4102
catalog.package.1234.asset.1.ca.product.0.rental_period=48

...

n items of the same format as shown above and below

catalog.package.****.asset_name=xyz
.
.
.
catalog.package.****.asset.1.ca.product.0.rental_period=48

...


catalog.package.5678.asset_name=I am the Superman
catalog.package.5678.asset_id=5678
catalog.package.5678.version_minor=75
catalog.package.5678.version_major=1
catalog.package.5678.custom_number=27
catalog.package.5678.custom.0.name=Title
catalog.package.5678.custom.0.value=I am the Superman
catalog.package.5678.custom.1.name=Summary_Short
catalog.package.5678.custom.1.value=blah!! blah!! Clah.. 2014
catalog.package.5678.custom.2.name=StartDate
catalog.package.5678.custom.2.value=2014-11-24 22:00:00
catalog.package.5678.custom.3.name=EndDate
catalog.package.5678.custom.3.value=2015-01-08 04:00:00
catalog.package.5678.custom.5.name=ReleaseDate
catalog.package.5678.custom.5.value=
catalog.package.5678.custom.6.name=Advisories
catalog.package.5678.custom.6.value=V
catalog.package.5678.custom.7.name=Actors
catalog.package.5678.custom.7.value=Sen,Navaneeth
catalog.package.5678.custom.8.name=Actors
catalog.package.5678.custom.9.value=Bautista,Dave
catalog.package.5678.custom.10.name=Genre
catalog.package.5678.custom.10.value=Movies/Comedy
catalog.package.5678.custom.11.name=Genre
catalog.package.5678.custom.11.value=Movies/All Movies
catalog.package.5678.asset_number=3
catalog.package.5678.asset.0.name=I am the Superman
catalog.package.5678.asset.0.asset_id=file://y.jpg
catalog.package.5678.asset.0.type=poster
catalog.package.5678.asset.0.version_minor=1
catalog.package.5678.asset.0.version_major=1
catalog.package.5678.asset.0.description=blah!! blah!! Clah.. 2014
catalog.package.5678.asset.1.name=BOXO_NG-NG-feature-Ingested-Scrambled
catalog.package.5678.asset.1.asset_id=qwerty5678
catalog.package.5678.asset.1.type=scrambled_video
catalog.package.5678.asset.1.version_minor=2
catalog.package.5678.asset.1.version_major=1
catalog.package.5678.asset.1.description=BOXO_NG-NG-feature-Ingested-Scrambled
catalog.package.5678.asset.1.custom_number=7
catalog.package.5678.asset.1.custom.0.name=Audio_Type
catalog.package.5678.asset.1.custom.0.value=Dolby Digital
catalog.package.5678.asset.1.custom.1.name=HDContent
catalog.package.5678.asset.1.custom.1.value=Y
catalog.package.5678.asset.1.custom.2.name=Dubbed_Languages
catalog.package.5678.asset.1.custom.2.value=
catalog.package.5678.asset.1.ca.product_number=1
catalog.package.5678.asset.1.ca.product.0.id=4102
catalog.package.5678.asset.1.ca.product.0.rental_period=48

With the help of some googling, I have written a code that can parse line by line of this file and get me the key and value pairs.

Please find the code below for that:

<?php

$catalogFile = "catalog.properties";

$b = parse_properties($catalogFile);

file_put_contents('filename.txt', print_r($b, true));

function parse_properties($txtProperties)
{

    $result = array();

    $handle = fopen($txtProperties, "r");
    if ($handle)
    {
        while (($line = fgets($handle)) !== false)
        {
            // process the line read.

            $key = "";
            $value = "";

            $isWaitingOtherLine = false;
            if (empty($line) || (!$isWaitingOtherLine && strpos($line, "#") === 0 && strpos($line, "//") === 0))
            {
                continue;
            }

            if (!$isWaitingOtherLine)
            {
                $key = trim(substr($line, 0, strpos($line, '=')));
                $value = trim(substr($line, strpos($line, '=') + 1, strlen($line)));
            }
            else
            {
                $value .= $line;
            }

            /* Check if ends with single '\' */
            if (strrpos($value, "\\") === strlen($value) - strlen("\\"))
            {
                $value = substr($value, 0, strlen($value) - 1) . "
";
                $isWaitingOtherLine = true;
            }
            else
            {
                $isWaitingOtherLine = false;
            }

            $result[$key] = $value;
        }
    }
    else
    {
        // error opening the file.
    }
    fclose($handle);

    return $result;
}

?> 

Now I need to create objects of type Asset parsing the properties file:

class Asset
{
    public $title;
    public $posterUrl;
    public $videoUrl;
    public $assetId;
    public $description;
    public $sDate;
    public $eDate;
    public $director;
    public $actor;
    public $genre;
    public $rating;
    public $channel;
    public $epSeason;
    public $epNumber;
    public $studio;
    public $fSize;
    public $audioType;
    public $hd;
    public $sf;
    public $subtitle;
    public $country;
}  

I need to populate each value in this class object while parsing this.

Now the problems are,

  • how to identify the start and end of each objects data
  • In cases as shown below:
catalog.package.1234.custom.0.name=Title
catalog.package.1234.custom.0.value=I am the Galaxians

I need to check if the value of catalog.package.1234.custom.0.name is "Title", populate the "Title" field in the object, with the catalog.package.1234.custom.0.value

And so on..

Every Object starts with the catalog.package.1234.asset_name and instead of "1234", it can be any unique number.

Please help me on this?


As asked for this is what I get with the above php code for file read line by line

[catalog.package.1234.asset_name] => I am the Galaxians
[catalog.package.1234.asset_id] => 1234
[catalog.package.1234.version_minor] => 75
[catalog.package.1234.version_major] => 1
[catalog.package.1234.custom_number] => 27
[catalog.package.1234.custom.0.name] => Title
[catalog.package.1234.custom.0.value] => I am the Galaxians
[catalog.package.1234.custom.1.name] => Summary_Short
[catalog.package.1234.custom.1.value] => blah!! blah!! blah.. 2014
[catalog.package.1234.custom.2.name] => StartDate
[catalog.package.1234.custom.2.value] => 2014-11-23 22:00:00
[catalog.package.1234.custom.3.name] => EndDate
[catalog.package.1234.custom.3.value] => 2015-01-07 04:00:00
[catalog.package.1234.custom.5.name] => ReleaseDate
[catalog.package.1234.custom.5.value] => 
[catalog.package.1234.custom.6.name] => Advisories
[catalog.package.1234.custom.6.value] => V
[catalog.package.1234.custom.7.name] => Actors
[catalog.package.1234.custom.7.value] => Pratt,Chris
[catalog.package.1234.custom.8.name] => Actors
[catalog.package.1234.custom.9.value] => Bautista,Dave
[catalog.package.1234.custom.10.name] => Genre
[catalog.package.1234.custom.10.value] => Movies/Action
[catalog.package.1234.custom.11.name] => Genre
[catalog.package.1234.custom.11.value] => Movies/All Movies
[catalog.package.1234.asset_number] => 3
[catalog.package.1234.asset.0.name] => I am the Galaxians
[catalog.package.1234.asset.0.asset_id] => file://x.jpg
[catalog.package.1234.asset.0.type] => poster
[catalog.package.1234.asset.0.version_minor] => 1
[catalog.package.1234.asset.0.version_major] => 1
[catalog.package.1234.asset.0.description] => blah!! blah!! blah.. 2014
[catalog.package.1234.asset.1.name] => BOXO_NG-NG-feature-Ingested-Scrambled
[catalog.package.1234.asset.1.asset_id] => qwerty1234
[catalog.package.1234.asset.1.type] => scrambled_video
[catalog.package.1234.asset.1.version_minor] => 2
[catalog.package.1234.asset.1.version_major] => 1
[catalog.package.1234.asset.1.description] => BOXO_NG-NG-feature-Ingested-Scrambled
[catalog.package.1234.asset.1.custom_number] => 7
[catalog.package.1234.asset.1.custom.0.name] => Audio_Type
[catalog.package.1234.asset.1.custom.0.value] => Dolby Digital
[catalog.package.1234.asset.1.custom.1.name] => HDContent
[catalog.package.1234.asset.1.custom.1.value] => Y
[catalog.package.1234.asset.1.custom.2.name] => Dubbed_Languages
[catalog.package.1234.asset.1.custom.2.value] => 
[catalog.package.1234.asset.1.ca.product_number] => 1
[catalog.package.1234.asset.1.ca.product.0.id] => 4102
[catalog.package.1234.asset.1.ca.product.0.rental_period] => 48

...

n items of the same format as shown above and below

[catalog.package.****.asset_name] => xyz
.
.
.
[catalog.package.****.asset.1.ca.product.0.rental_period] => 48

...


[catalog.package.5678.asset_name] => I am the Superman
[catalog.package.5678.asset_id] => 5678
[catalog.package.5678.version_minor] => 75
[catalog.package.5678.version_major] => 1
[catalog.package.5678.custom_number] => 27
[catalog.package.5678.custom.0.name] => Title
[catalog.package.5678.custom.0.value] => I am the Superman
[catalog.package.5678.custom.1.name] => Summary_Short
[catalog.package.5678.custom.1.value] => blah!! blah!! Clah.. 2014
[catalog.package.5678.custom.2.name] => StartDate
[catalog.package.5678.custom.2.value] => 2014-11-24 22:00:00
[catalog.package.5678.custom.3.name] => EndDate
[catalog.package.5678.custom.3.value] => 2015-01-08 04:00:00
[catalog.package.5678.custom.5.name] => ReleaseDate
[catalog.package.5678.custom.5.value] => 
[catalog.package.5678.custom.6.name] => Advisories
[catalog.package.5678.custom.6.value] => V
[catalog.package.5678.custom.7.name] => Actors
[catalog.package.5678.custom.7.value] => Sen,Navaneeth
[catalog.package.5678.custom.8.name] => Actors
[catalog.package.5678.custom.9.value] => Bautista,Dave
[catalog.package.5678.custom.10.name] => Genre
[catalog.package.5678.custom.10.value] => Movies/Comedy
[catalog.package.5678.custom.11.name] => Genre
[catalog.package.5678.custom.11.value] => Movies/All Movies
[catalog.package.5678.asset_number] => 3
[catalog.package.5678.asset.0.name] => I am the Superman
[catalog.package.5678.asset.0.asset_id] => file://y.jpg
[catalog.package.5678.asset.0.type] => poster
[catalog.package.5678.asset.0.version_minor] => 1
[catalog.package.5678.asset.0.version_major] => 1
[catalog.package.5678.asset.0.description] => blah!! blah!! Clah.. 2014
[catalog.package.5678.asset.1.name] => BOXO_NG-NG-feature-Ingested-Scrambled
[catalog.package.5678.asset.1.asset_id] => qwerty5678
[catalog.package.5678.asset.1.type] => scrambled_video
[catalog.package.5678.asset.1.version_minor] => 2
[catalog.package.5678.asset.1.version_major] => 1
[catalog.package.5678.asset.1.description] => BOXO_NG-NG-feature-Ingested-Scrambled
[catalog.package.5678.asset.1.custom_number] => 7
[catalog.package.5678.asset.1.custom.0.name] => Audio_Type
[catalog.package.5678.asset.1.custom.0.value] => Dolby Digital
[catalog.package.5678.asset.1.custom.1.name] => HDContent
[catalog.package.5678.asset.1.custom.1.value] => Y
[catalog.package.5678.asset.1.custom.2.name] => Dubbed_Languages
[catalog.package.5678.asset.1.custom.2.value] => 
[catalog.package.5678.asset.1.ca.product_number] => 1
[catalog.package.5678.asset.1.ca.product.0.id] => 4102
[catalog.package.5678.asset.1.ca.product.0.rental_period] => 48
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    duanchou6534 duanchou6534 2014-12-31 13:43

    I have finally come up with my own solution for this.

    <?php
    
        require('Asset.php');
    
        $vodAssetArray = array();
    
        $catalogMajorVersion = "-1";
        $catalogMinorVersion = "-1";
        $catalogCreated = "-1";
    
        $catalogFile = "catalog.properties";
    
        $b = parse_properties($catalogFile);
    
        $c = array();
    
        foreach ($b as $key => $values)
        {
            echo $key . "=" . $values . "<br />";
            if ($key === "catalog.package_asset_id_list")
            {
                $c = explode(",", $values);
            }
        }
    
    
        foreach ($c as $key_c => $values_c)
        {
            $vodAssetObject = new PushVodAsset();
    
            $actorsList = array();
            $directorList = array();
            $genreList = array();
            $advisoriesList = array();
    
            foreach ($b as $key_b => $values_b)
            {
                if (strpos($key_b, $values_c) !== false)
                {
    
                    if (endsWith($key_b, ".name"))
                    {
    //                    echo $values_b . "<br />";
    
    //                    echo $key_b . "<br />";
    
                        if ($values_b === "Title")
                        {
                            $key_val = substr_replace($key_b, ".value", strpos($key_b, ".name"));
                            $vodAssetObject->title = $b[$key_val];
    //                        echo $b[$key_val] . "<br />";
                        }
    
                        else if ($values_b === "Summary_Short")
                        {
                            $key_val = substr_replace($key_b, ".value", strpos($key_b, ".name"));
                            $vodAssetObject->description = $b[$key_val];
    //                        echo $b[$key_val] . "<br />";
                        }
    
                        else if ($values_b === "StartDate")
                        {
                            $key_val = substr_replace($key_b, ".value", strpos($key_b, ".name"));
                            $vodAssetObject->sDate = $b[$key_val];
    //                        echo $b[$key_val] . "<br />";
                        }
    
                        else if ($values_b === "EndDate")
                        {
                            $key_val = substr_replace($key_b, ".value", strpos($key_b, ".name"));
                            $vodAssetObject->eDate = $b[$key_val];
    //                        echo $b[$key_val] . "<br />";
                        }
    
                        else if ($values_b === "Director")
                        {
                            $key_val = substr_replace($key_b, ".value", strpos($key_b, ".name"));
                            array_push($directorList, $b[$key_val]);
                            $vodAssetObject->director = $directorList;
    //                        echo $b[$key_val] . "<br />";
                        }
    
                        else if ($values_b === "Actors")
                        {
                            $key_val = substr_replace($key_b, ".value", strpos($key_b, ".name"));
                            array_push($actorsList, $b[$key_val]);
                            $vodAssetObject->actor = $actorsList;
    //                        echo $b[$key_val] . "<br />";
                        }
    
                        else if ($values_b === "Genre")
                        {
                            $key_val = substr_replace($key_b, ".value", strpos($key_b, ".name"));
                            array_push($genreList, $b[$key_val]);
                            $vodAssetObject->genre = $genreList;
    //                        echo $b[$key_val] . "<br />";
                        }
    
                        else if ($values_b === "Advisories")
                        {
                            $key_val = substr_replace($key_b, ".value", strpos($key_b, ".name"));
                            array_push($advisoriesList, $b[$key_val]);
                            $vodAssetObject->advisories = $advisoriesList;
    //                        echo $b[$key_val] . "<br />";
                        }
                    }
    
                    else if (endsWith($key_b, ".asset_id"))
                    {
                        $vodAssetObject->assetId = $values_b;
                    }
    
                    else if (endsWith($key_b, ".type"))
                    {
                        if ($values_b === "poster")
                        {
                            $key_val = substr_replace($key_b, ".asset_id", strpos($key_b, ".type"));
                            $vodAssetObject->posterUrl = $b[$key_val];
    //                        echo $b[$key_val] . "<br />";
                        }
                    }
                }
            }
    
            //Add this item to an ArrayList
            array_push($vodAssetArray, $vodAssetObject);
        }
    
        //Put this into Tmp folder for access
    
        function parse_properties($txtProperties)
        {
    
            $result = array();
    
            $handle = fopen($txtProperties, "r");
            if ($handle)
            {
                while (($line = fgets($handle)) !== false)
                {
                    // process the line read.
    
                    $key = "";
                    $value = "";
    
                    $isWaitingOtherLine = false;
                    if (empty($line) || (!$isWaitingOtherLine && strpos($line, "#") === 0 && strpos($line, "//") === 0))
                    {
                        continue;
                    }
    
                    if (!$isWaitingOtherLine)
                    {
                        $key = trim(substr($line, 0, strpos($line, '=')));
                        $value = trim(substr($line, strpos($line, '=') + 1, strlen($line)));
                    }
                    else
                    {
                        $value .= $line;
                    }
    
                    /* Check if ends with single '\' */
                    if (strrpos($value, "\\") === strlen($value) - strlen("\\"))
                    {
                        $value = substr($value, 0, strlen($value) - 1) . "
    ";
                        $isWaitingOtherLine = true;
                    }
                    else
                    {
                        $isWaitingOtherLine = false;
                    }
    
                    $result[$key] = $value;
                }
            }
            else
            {
                // error opening the file.
            }
            fclose($handle);
    
            return $result;
        }
    
        function endsWith($string, $subString)
        {
            $strLen = strlen($string);
            $subStringLen = strlen($subString);
            if ($subStringLen > $strLen)
            {
                return false;
            }
            return substr_compare($string, $subString, $strLen - $subStringLen, $subStringLen) === 0;
        }
    
    ?>
    
    点赞 评论 复制链接分享

相关推荐