0

I've tried to parse a XML String with php.

The example of XML is

<?xml version="1.0"?>
<root>
    <Log>
        <Item>
            <name>![CDATA[James]]</name>
            <address>![CDATA[Korea]]</address>
            <email>![CDATA[[email protected]]]</email>
        </Item>
        <Item>
            <name>![CDATA[James2]]</name>
            <address>![CDATA[Korea2]]</address>
            <email>![CDATA[[email protected]]]</email>
        </Item>
    </Log>
</root>

My Parsing PHP Code is

$xml = simplexml_load_string($input,null,LIBXML_NOCDATA);
$xml=json_decode( json_encode( $xml),true);

However, When I try to get sizeof($xml['Log']['Item']), have some problems.

If XML have One 'Item' , then sizeof($xml['Log']['Item']) return '3' .

but If XML have 4 'Item's , then it return '4'.

I want to return 1, if XML have One 'Item'.

How can I solve this problem.?

4

1 Answer 1

1

I solved this parsing problem with foreach().

$xml = simplexml_load_string($input,null,LIBXML_NOCDATA);
$itemArray=$xml->Log->Item;
$i=0;
$sqlParamArray= array();
foreach ($itemArray as $tag) {
 $name= trim($tag->name);
 $address= trim($tag->address);
 $email= trim($tag->email);
  array_push($sqlParamArray,$name,$address,$email);
  $i++;
}
echo $i;

....

sizeof('Item') is saved in $i.

I solved it.

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.