How can I use simplexml to parse this xml? vardump is always false.

Source: Internet
Author: User
Tags printable characters
Could you tell me how to use simplexml to parse this xml? vardump is always a "false" moderator and I am asking for help again... This is the XML data returned by the webpage. I don't know why simplexml cannot be used for reading. var_dump displays false.

echo '#########################'.'

';var_dump($xml); echo '#########################';echo '
'.'
'.'
'.$xml->xsm->nickname;

If the above text is printed directly like this:

echo '#########################'.'

';var_dump(simplexml_load_string($xml); echo '#########################';echo '
'.'
'.'
'.$xml->xsm->nickname;

The bool (false) is displayed when you print the code)
Solution


Reply to discussion (solution)

$xml = preg_replace('/!|--/', '', $xml);var_dump(simplexml_load_string($xml));

$xml = preg_replace('/!|--/', '', $xml);var_dump(simplexml_load_string($xml));


//$xml='
 ';$xml = preg_replace('/!--\?|--/','', $xml);var_dump($xml);

In this case, I tried to make a positive match for a single row, but it is invalid in this code. The returned value still has a annotator in the element viewer of the webpage.

Why are you smart?

$xml = preg_replace('/!|--/', '', $xml);var_dump(simplexml_load_string($xml));


I didn't understand the comment in xml before. now I understand it. so I thought of another method, but it still failed. can you tell me the reason?
$xml = '
 
  '.$xml;var_dump(simplexml_load_string($xml));
 

I added a line of xml file tag using the string operator, but it still failed to display when simplexml_load_string was used.

Why do you want to be smart?

Why are you smart?


The code you posted to me cannot be loaded...

That's because you don't give me your data.

Why do you want to be smart?



This is the result of directly copying the code to me and sticking it to the running code...

You still have to remove the simplexml load statement before dumping it out.

You are useless!
How do you know there are no printable characters in it?

That's because you don't give me your data.


 
 
  
   
114
  
  
   
Wedc
  
  
   
2
  
  
   
116
  
  
   
11601
  
  
   
1062014
  
  
  
   
V = 2014080600
  
  
   
02
  
  
  
   
11620001
  
  
   
1407488228735
  
  
   
10116226011162288228735
  
  
   
C6959b4eacf7b2f
  
 

This is the $ xml value I wrote using fwrite.

The file tag is not commented out, so I don't know what to ask.

You are useless!
How do you know there are no printable characters in it?


View it in IE
The end tag 'xsm 'does not match the start tag 'comshort.

SimpleXMLElement Object ([@ attributes] => Array ([code] => 0000 [msg] => verification successful abc [trans_time] => 20140808162708) [userId] => 114 [nickName] => wedc [userType] => 2 [comId] => 116 [saledptId] => 11601 [refId] => 1062014 [comName] => SimpleXMLElement Object () [domainUrl] => v = 2014080600 [comType] => 02 [comShort] => SimpleXMLElement Object () [parentComId] => 11620001 [expirationTime] => 1407488228735 [planText] => 10116226011162288228735 [signatureValue] => c6959b4eacf7b2f)
Isn't that all right?

The length of the string is 984 bytes, and the size of the string is only 509 bytes.
Where are more than four hundred bytes?

You are useless!
How do you know there are no printable characters in it?


Moderator, I know, the reason is that it contains Chinese characters so it cannot be loaded. how can this problem be solved? I cannot change the data because it is not my own.

SimpleXMLElement Object ([@ attributes] => Array ([code] => 0000 [msg] => verification successful abc [trans_time] => 20140808162708) [userId] => 114 [nickName] => wedc [userType] => 2 [comId] => 116 [saledptId] => 11601 [refId] => 1062014 [comName] => SimpleXMLElement Object () [domainUrl] => v = 2014080600 [comType] => 02 [comShort] => SimpleXMLElement Object () [parentComId] => 11620001 [expirationTime] => 1407488228735 [planText] => 10116226011162288228735 [signatureValue] => c6959b4eacf7b2f)
Isn't that all right?

The length of the string is 984 bytes, and the size of the string is only 509 bytes.
Where are more than four hundred bytes?


Another 400 bytes is a long string that is too long. I deleted half of it.
Thank you. I found the cause. it is because PHP uses utf8 encoding. when we get xml data, we need to change the gbk in the file to utf8.

SimpleXMLElement Object ([@ attributes] => Array ([code] => 0000 [msg] => verification successful abc [trans_time] => 20140808162708) [userId] => 114 [nickName] => wedc [userType] => 2 [comId] => 116 [saledptId] => 11601 [refId] => 1062014 [comName] => SimpleXMLElement Object () [domainUrl] => v = 2014080600 [comType] => 02 [comShort] => SimpleXMLElement Object () [parentComId] => 11620001 [expirationTime] => 1407488228735 [planText] => 10116226011162288228735 [signatureValue] => c6959b4eacf7b2f)
Isn't that all right?

The length of the string is 984 bytes, and the size of the string is only 509 bytes.
Where are more than four hundred bytes?
Succeeded ~~~~~~ []

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.