Avatar billede skizo_someone Nybegynder
12. juli 2004 - 21:00 Der er 4 kommentarer og
1 løsning

Tage værdier fra en xml fil og smække det ind i et array

Jeg har en stor xml fil som indeholder en række kundeinformationer fra en onlinebutik. XML filen indeholder personlige informationer om kunden og deres køb. Det jeg skal have gjort er, at jeg skal have isoleret de kunder hvor ORDERSTATE = 2 og så skal jeg have smidt deres informationer ind i et array så jeg kan lege med dem der.

XML filen indeholder mange hundrede kunder, men følgende eksempel består af 2 kunder og deres køb (personlige informationer er ændret)
http://files.pornogris.dk/order-export.xml

Altså! det jeg skal have, er et script som kan isolere de kunder hvor ORDERSTATE = 2, og så skal den tage informationen fra de udvalgte kunder og smide ind i et array, så man nemt kan hente dem frem igen.

Jeg har ikke selv den store erfaring med XML i PHP, eller bare XML for den sags skyld, derfor vil jeg give 200 point til den som kan give mig et script, eller hjælpe mig så groft på vej, at jeg selv kan finde ud af det.
Avatar billede googolplex Novice
12. juli 2004 - 22:35 #1
Jeg ved ikke meget om xml i forbindelse med php, men siden ingen er kommet med et bud vil jeg ligge disse statiske metoder. Måske kan du bruge dem:

<?php
// {{{ toString()
/**
* This method converts a file to a string. It returns an Error object if it is unable to open the file.
*
* @param    fileName String. The name of the file to convert.
*
* @return    String
* @author  simgar
*/

function & toString( $fileName )
{
    if ($content_array = file($fileName))
    {
        return implode("", $content_array);
    }
    else
    {
        // Error
        return false;
    }
}
// }}}

// {{{ xmlFileToArray()
/**
* This static method converts an xml file to an associative array
* duplicating the xml file structure.
*
* @param    $fileName. String. The name of the xml file to convert.
*            This method returns an Error object if this file does not
*            exist or is invalid.
* @param    $includeTopTag. booleal. Whether or not the topmost xml tag
*            should be included in the array. The default value for this is false.
* @param    $lowerCaseTags. boolean. Whether or not tags should be
*            set to lower case. Default value for this parameter is true.
* @access    public static
* @return    Associative Array
* @author    simgar
*/
function & xmlFileToArray($fileName, $includeTopTag = false, $lowerCaseTags = true)
{
    // Definition file not found
    if (!file_exists($fileName))
    {
        return Error::logError("File::xmlFileToArray: Failed - File '" . $fileName .
                              "' does not exist", __FILE__, __LINE__, SRA_ERROR_PROBLEM);
    }
    $p = xml_parser_create();
    xml_parse_into_struct($p,File::toString($fileName),$vals,$index);
    xml_parser_free($p);
    $xml = array();
    $levels = array();
    $multipleData = array();
    $prevTag = "";
    $currTag = "";
    $topTag = false;
    foreach ($vals as $val)
    {
        // Open tag
        if ($val["type"] == "open")
        {
            if (!File::_xmlFileToArrayOpen($topTag, $includeTopTag, $val, $lowerCaseTags,
                                          $levels, $prevTag, $multipleData, $xml))
            {
                continue;
            }
        }
        // Close tag
        else if ($val["type"] == "close")
        {
            if (!File::_xmlFileToArrayClose($topTag, $includeTopTag, $val, $lowerCaseTags,
                                            $levels, $prevTag, $multipleData, $xml))
            {
                continue;
            }
        }
        // Data tag
        else if ($val["type"] == "complete" && isset($val["value"]))
        {
            $loc =& $xml;
            foreach ($levels as $level)
            {
                $temp =& $loc[str_replace(":arr#", "", $level)];
                $loc =& $temp;
            }
            $tag = $val["tag"];
            if ($lowerCaseTags)
            {
                $tag = strtolower($val["tag"]);
            }
            $loc[$tag] = str_replace("\\n", "\n", $val["value"]);
        }
        // Tag without data
        else if ($val["type"] == "complete")
        {
            File::_xmlFileToArrayOpen($topTag, $includeTopTag, $val, $lowerCaseTags,
                                      $levels, $prevTag, $multipleData, $xml);
            File::_xmlFileToArrayClose($topTag, $includeTopTag, $val, $lowerCaseTags,
                                      $levels, $prevTag, $multipleData, $xml);
        }
    }
    return $xml;
}
// }}}

// {{{ _xmlFileToArrayOpen()
/**
* Private support function for File::xmlFileToArray. Handles an xml OPEN tag.
*
* @param    $topTag. String. xmlFileToArray topTag variable
* @param    $includeTopTag. boolean. xmlFileToArray includeTopTag variable
* @param    $val. String[]. xmlFileToArray val variable
* @param    $currTag. String. xmlFileToArray currTag variable
* @param    $lowerCaseTags. boolean. xmlFileToArray lowerCaseTags variable
* @param    $levels. String[]. xmlFileToArray levels variable
* @param    $prevTag. String. xmlFileToArray prevTag variable
* @param    $multipleData. boolean. xmlFileToArray multipleData variable
* @param    $xml. String[]. xmlFileToArray xml variable
* @access    private static
* @return    boolean
* @author    simgar
*/
function _xmlFileToArrayOpen(& $topTag, & $includeTopTag, & $val, & $lowerCaseTags,
                            & $levels, & $prevTag, & $multipleData, & $xml)
{
    // don't include top tag
    if (!$topTag && !$includeTopTag)
    {
        $topTag = $val["tag"];
        return false;
    }
    $currTag = $val["tag"];
    if ($lowerCaseTags)
    {
        $currTag = strtolower($val["tag"]);
    }
    $levels[] = $currTag;
   
    // Multiple items w/ same name. Convert to array.
    if ($prevTag === $currTag)
    {
        if (!array_key_exists($currTag, $multipleData) ||
            !$multipleData[$currTag]["multiple"])
        {
            $loc =& $xml;
            foreach ($levels as $level)
            {
                $temp =& $loc[$level];
                $loc =& $temp;
            }
            $loc = array($loc);
            $multipleData[$currTag]["multiple"] = true;
            $multipleData[$currTag]["multiple_count"] = 0;
        }
        $multipleData[$currTag]["popped"] = false;
        $levels[] = ":arr#" . ++$multipleData[$currTag]["multiple_count"];
    }
    else
    {
        $multipleData[$currTag]["multiple"] = false;
    }
   
    // Add attributes array
    if (array_key_exists("attributes", $val))
    {
        $loc =& $xml;
        foreach ($levels as $level)
        {
            $temp =& $loc[str_replace(":arr#", "", $level)];
            $loc =& $temp;
        }
        $keys = array_keys($val["attributes"]);
        foreach ($keys as $key)
        {
            $tag = $key;
            if ($lowerCaseTags)
            {
                $tag = strtolower($tag);
            }
            $loc["attributes"][$tag] = & $val["attributes"][$key];
        }
    }
    return true;
}
// }}}

// {{{ _xmlFileToArrayClose()
/**
* Private support function for File::xmlFileToArray. Handles an xml OPEN tag.
*
* @param    $topTag. String. xmlFileToArray topTag variable
* @param    $includeTopTag. boolean. xmlFileToArray includeTopTag variable
* @param    $val. String[]. xmlFileToArray val variable
* @param    $currTag. String. xmlFileToArray currTag variable
* @param    $lowerCaseTags. boolean. xmlFileToArray lowerCaseTags variable
* @param    $levels. String[]. xmlFileToArray levels variable
* @param    $prevTag. String. xmlFileToArray prevTag variable
* @param    $multipleData. boolean. xmlFileToArray multipleData variable
* @param    $xml. String[]. xmlFileToArray xml variable
* @access    private static
* @return    boolean
* @author    simgar
*/
function _xmlFileToArrayClose(& $topTag, & $includeTopTag, & $val, & $lowerCaseTags,
                              & $levels, & $prevTag, & $multipleData, & $xml)
{
    // don't include top tag
    if ($topTag && !$includeTopTag && $val["tag"] == $topTag)
    {
        return false;
    }
    if ($multipleData[$currTag]["multiple"])
    {
        $tkeys = array_reverse(array_keys($multipleData));
        foreach ($tkeys as $tkey)
        {
            if ($multipleData[$tkey]["multiple"] && !$multipleData[$tkey]["popped"])
            {
                array_pop($levels);
                $multipleData[$tkey]["popped"] = true;
                break;
            }
            else if (!$multipleData[$tkey]["multiple"])
            {
                break;
            }
        }
    }
    $prevTag = array_pop($levels);
    if (strpos($prevTag, "arr#"))
    {
        $prevTag = array_pop($levels);
    }
    return true;
}
// }}}
?>
Avatar billede googolplex Novice
12. juli 2004 - 22:38 #2
Har taget metoderne fra codewalkers.com, hvor der ligger en opdateret udgave:

http://codewalkers.com/seecode.php?id=185
Avatar billede coderdk Praktikant
12. juli 2004 - 22:38 #3
Du kan bruge DOM XML og en løkke ;) Evt fjerne dem der ikke har orderstate = 2

http://dk.php.net/domxml
Avatar billede skizo_someone Nybegynder
13. juli 2004 - 21:21 #4
undskyld min langsomme respons...
googolplex > Scriptet virker ikke, når jeg prøver det får jeg følgende fejlmedelse:
Fatal error: Undefined class name 'error'

coderdk > jeg syntes ikke lige jeg kan overskue brugen af DOMXML modulet...
Avatar billede skizo_someone Nybegynder
03. september 2004 - 12:55 #5
jeg fandt aldrig ud af det, og droppede projektet... jeg lukker
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester