0

i am having some problem with parsing XML.

What i am doing: I am spliting 1 XML to many and also editing few things for new one. And it works!!

Where is problem: After Parsing not all parts are same, to be more specifc in some of them are just 2 Labor Rate Details, in another there can be 5.:

So some looks like:

  <LaborRateList>
    <LaborRateDetail>
     <Role>Labor</Role>
     <Category>1</Category>
     <Rate Cur="CZK" Unit="h" MinValue="0" MaxValue="0">250.0</Rate>
    </LaborRateDetail>
    <LaborRateDetail>
     <Role>Paint</Role>
     <Category>2</Category>
     <Rate Cur="CZK" Unit="h" MinValue="0" MaxValue="0">350.0</Rate>
    </LaborRateDetail>
   </LaborRateList>

And another:

   <LaborRateList>
    <LaborRateDetail>
     <Role>Labor</Role>
     <Category>1</Category>
     <Rate Cur="CZK" Unit="h" MinValue="0" MaxValue="0">250.0</Rate>
    </LaborRateDetail>
    <LaborRateDetail>
     <Role>Labor</Role>
     <Category>1</Category>
     <Rate Cur="CZK" Unit="h" MinValue="0" MaxValue="0">250.0</Rate>
    </LaborRateDetail>
    <LaborRateDetail>
     <Role>Labor</Role>
     <Category>1</Category>
     <Rate Cur="CZK" Unit="h" MinValue="0" MaxValue="0">250.0</Rate>
    </LaborRateDetail>
    <LaborRateDetail>
     <Role>Labor</Role>
     <Category>1</Category>
     <Rate Cur="CZK" Unit="h" MinValue="0" MaxValue="0">250.0</Rate>
    </LaborRateDetail>
    <LaborRateDetail>
     <Role>Paint</Role>
     <Category>2</Category>
     <Rate Cur="CZK" Unit="h" MinValue="0" MaxValue="0">350.0</Rate>
    </LaborRateDetail>
   </LaborRateList>

i was trying to find out how to not insert anything if there is no more labor rates. What i try was check if there is anything in variabile and if not write nothing so my code looks like:

//RATE3 PART     
var_dump($value->Contract->LaborRateList->LaborRateDetail[2]->Role);
    if ($value->Contract->LaborRateList->LaborRateDetail[2]->Role == 0)
//SOME CODE HERE

But i am getting error:

Notice: Trying to get property of non-object in C:\xampp\htdocs\xampp\provisioning\index.php on line 23
NULL 
Notice: Trying to get property of non-object in C:\xampp\htdocs\xampp\provisioning\index.php on line 24

My FULL CODE can be found on http://pastebin.com/12HiHEcu , it's my last try which is not working :(

Can somebody help me to fix it?

3
  • So how are you parsing them? You can't just access an XML node in a "normal" string. You will need to use some kind of library to parse it. Commented Nov 11, 2014 at 10:49
  • EDITED and added full code Commented Nov 11, 2014 at 10:55
  • This is easier with DOM (because of the node methods), see: stackoverflow.com/questions/26774674/… Commented Nov 11, 2014 at 13:30

1 Answer 1

0

If i understand correctly, you could use another foreach to do that:

foreach($value->Contract->LaborRateList->LaborRateDetail as $laborRateDetails) {
  //Do your stuff
}
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.