Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9088293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:52:54+00:00 2026-06-16T21:52:54+00:00

Hi Can you help me with parsing XML elements I am trying to get

  • 0

Hi Can you help me with parsing XML elements
I am trying to get values from XML and write it to csv file, so far I can read everything but inner elements in XML are confusing to me, so If you can give ideas how to solve this?

I have a XML:

          <Product>
            <ProductCode>51629AE</ProductCode>
            <Vendor>HEWLETT PACKARD</Vendor>
            <ProductType>Printer Ink Cartridge</ProductType>
            <ProductCategory>Printeri</ProductCategory>
            <ProductDescription>Ink Cartridge HEWLETT PACKARD Black 40ml for HP DJ600/660/670/680/690/DW 600/660/680/690/OJ500/600/700/PSC-370/380/FAX 910 (650pages)</ProductDescription>
            <Image>https://www.it4profit.com/catalogimg/wic/1/51629AE</Image>
            <ProductCard>https://content.it4profit.com/itshop/itemcard_cs.jsp?ITEM=50409104044691930&amp;THEME=asbis&amp;LANG=lv</ProductCard>
            <AttrList>
              <element Name="Ink Color" Value="Black"/>
              <element Name="Ink Capacity" Value="40 ml"/>
              <element Name="Compliance" Value="HP DJ600/660/670/680/690/DW 600/660/680/690/OJ500/600/700/PSC-370/380/FAX 910"/>
              <element Name="Estimated Maximum Cartridge Life" Value="650 pages"/>
              <element Name="Number of Ink Colors" Value="1"/>
              <element Name="Typical Page Coverage" Value="5 %"/>
              <element Name="Warranty Products returnable" Value="Yes"/>
              <element Name="Pack Weight Brutto (kg)" Value="0.1 kg"/>
              <element Name="Pieces in pack" Value="1"/>
            </AttrList>
            <MarketingInfo>
              <element>The HP no. 29 black inkjet print cartridge is ideal for business or home users who want professional-quality printouts. The HP no. 29 is a 600-dpi black pigmented inkjet print cartridge that delivers sharp, professional quality documents and is made to work with HP colour inks for maximum results. Output is consistently crisp and clean on almost any plain paper. The print cartridge contains genuine HP ink that provides quality and reliability every time you print.</element>
            </MarketingInfo>
            <Images>
              <Image>https://content.it4profit.com/pimg/s/resize/160x160x160x160/2783330.jpg</Image>
            </Images>
          </Product>

This data is just example I need to take whatever is there and write it to csv.
This part is making problems to me. I don’t know how to extract this data from this XML file
Now when it comes to this script breaks and that is the problem
And have problems getting this data:

            <AttrList>
              <element Name="Ink Color" Value="Black"/>
              <element Name="Ink Capacity" Value="40 ml"/>
              <element Name="Compliance" Value="HP DJ600/660/670/680/690/DW 600/660/680/690/OJ500/600/700/PSC-370/380/FAX 910"/>
              <element Name="Estimated Maximum Cartridge Life" Value="650 pages"/>
              <element Name="Number of Ink Colors" Value="1"/>
              <element Name="Typical Page Coverage" Value="5 %"/>
              <element Name="Warranty Products returnable" Value="Yes"/>
              <element Name="Pack Weight Brutto (kg)" Value="0.1 kg"/>
              <element Name="Pieces in pack" Value="1"/>
            </AttrList>

I need to save name and value for that name

This is my code that worsk but not for elements. That is missing and I need to figure out how to implement it in this code:

    function parsing_xml($supplier_name,$xml_url,$opsi='string'){
        $xml = simplexml_load_file(utf8_encode($xml_url), 'SimpleXMLElement', LIBXML_NOCDATA);
        //$xml = simplexml_load_file($xml_url, 'SimpleXMLElement', LIBXML_NOCDATA);
        if(!$xml){echo "xml $xml_url not loaded.";exit;}
        $i=0;
        $string="";
        foreach($xml as $k=>$v){
            $b="";
            if($i==0){
                foreach($v as $k1=>$v1){
                    $head[]=$k1;
                }
                array_push($head,"ime_proizvoda","meta_description","meta_tag","tax","supplier","quantity","price","Referenca","tezina","opis_link");
                //array_push($head,"dugi_opis","ime_proizvoda","meta_description","meta_tag","tax","supplier","quantity","price","Referenca","tezina");
                foreach($head as $k4=>$v4){
                    $b.= "$v4".DELIMITER;
                }
            }//echo $xml->$k->BrojArtikla."=>".$xml->$k->Image."<pre>".print_r($head,1)."</pre>";
            foreach($head as $k3=>$v3){
                if($v3=='meta_description'){$v3='Naziv';}

                if(empty($v->$v3) or $v->$v3=="" or !isset($v->$v3)){
                    $v->$v3="|||";
                }
                if($v3=='tax'){
                    $b.=TAX.DELIMITER;
                    $v->$v3=TAX;
                }elseif($v3=='supplier'){
                    $b.=$supplier_name.DELIMITER;
                    $v->$v3=$supplier_name;
                }elseif($v3=='quantity'){
                    if($v->Stanje=="+"){
                    //if($v->Status=='Raspoloživo'){
                        $b.="4".DELIMITER;
                        $v->$v3=2;
                    }else{
                        $b.="0".DELIMITER;
                        $v->$v3=0;
                    }
                }elseif($v3=='Cijena'){
                    //$b.=(string) floatval($v->$v3).DELIMITER;
                    //$v->$v3=(string) floatval($v->$v3);

                    $b.=str_replace(',', '.', (string) $v->$v3).DELIMITER;
                    $v->$v3=str_replace(',', '.', (string) $v->$v3);



                    //$b.= str_replace(',', '.', floatval($v->$v3)).DELIMITER;
                    //$v->$v3= str_replace(',', '.', floatval($v->$v3));
                }elseif($v3=='ListPrice'){
                    $b.= (string) floatval($v->$v3).DELIMITER;
                    $v->$v3= (string) floatval($v->$v3);
                }elseif($v3=='Rabat'){
                    $b.= (string) floatval($v->$v3).DELIMITER;
                    $v->$v3= (string) floatval($v->$v3);
                }elseif($v3=='price'){
                    $b.= (string) floatval($v->Cijena)*MARZA.DELIMITER;
                    $v->$v3=(string) floatval($v->Cijena)*MARZA;
                }


                elseif($v3=='meta_tag'){
                    $b.=$v->$v->Brand.DELIMITER;
                    $v->$v3=$v->$v->Brand;
                }

                //ovo za ime da makne cudne znakove

                 elseif($v3=='ime_proizvoda'){


                 $b.=(string) preg_replace('/[%&#\\/-]/', '', $v->Naziv).DELIMITER;
                 //
                 //
                 //
                  $v->$v3=(string) preg_replace('/[%&#\\/-]/', '', $v->Naziv);


                    }

                    //ovo za referencu makni prva 4 znaka
                 elseif($v3=='Referenca'){

                 $b.=substr($v->Sifra, 4).DELIMITER;



                   $v->$v3=substr($v->Sifra, 4);


                    }

                      //ovo opis sa linkom
                 elseif($v3=='opis_link'){

                 $b.=$v->$v->Opis."<br/>Link na proizvod: <p><a href=$v->Link>$v->Link</a></p>".DELIMITER;
                 $v->$v3=$v->Opis."<br/>Link na proizvod: <p><a href=$v->Link>$v->Link</a></p>";



                    }

                    elseif($v3=='tezina'){

                 $b.=str_replace(',', '.', $v->Bruto_tezina_kg).DELIMITER;



                   $v->$v3=str_replace(',', '.', $v->Bruto_tezina_kg);;


                    }

                else{
                    $b.=$v->$v3.DELIMITER;
                }
            }
            $supplier_item[(string)$v->Sifra]=$v;
            $b=substr($b,0,-1);
            $b=str_replace("'","\'",$b);
            $b=str_replace('"','\"',$b);
            $b=str_replace("\n"," ",$b);

            $i++;
            $string.="\n$b";

        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T21:52:55+00:00Added an answer on June 16, 2026 at 9:52 pm

    You should be able to get the attributes of a simpleXmlElement with $element->attributes(), this returns an associative array with the attribute names and values, in your example:

    "Name" => "Ink Color"
    "Value" => "Black"
    

    See http://php.net/manual/en/simplexmlelement.attributes.php for the documentation of the SimpleXmlElement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing data from an xml file using WebClient. In DownloadStringCompleted method I have
I'm new to xml parsing, I am trying to parse the following xml file
I am trying to insert data of Books by parsing XML file. It allows
I am parsing a XML file and I want to insert the parsed values
I've got a string Purchases 10384839,Purchases 10293900,Purchases 20101024 Can anyone help me with parsing
Anyone can help me? Trying to Add a Slow function with my smoothscroll and
Who can help me to translate this XML Schema pattern [0-9]+-([0-9]|K) to java regular
I'm having some trouble parsing an XML file in Java. The file takes the
I want to create custom tag, but i get XML parsing error on JSPVersion
I am using the following code for parsing an XML file. But I don't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.