Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 请问这个xml怎么用simplexml解析,vardump总是false

请问这个xml怎么用simplexml解析,vardump总是false

Jun 23, 2016 pm 01:52 PM
false simplexml xml parse

版主,又来求助了。。。这回事是网页返回的XML数据,我不知道为什么不能用simplexml读取,var_dump显示false

	echo '#########################'.'</br></br>';	var_dump($xml); 	echo '#########################';	echo '<br>'.'<br>'.'<br>'.$xml->xsm->nickname;
Copy after login

如果用上面这段直接打印是这样子的:

	echo '#########################'.'</br></br>';	var_dump(simplexml_load_string($xml); 	echo '#########################';	echo '<br>'.'<br>'.'<br>'.$xml->xsm->nickname;
Copy after login

用这段代码打印就会显示bool(false)
求解决


回复讨论(解决方案)

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

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


	//$xml='<!--?xml version="1.0" encoding="gbk"?-->';	$xml = preg_replace('/!--\?|--/','', $xml);	var_dump($xml);
Copy after login

这样我试了,单独一行可以正匹配,但是在这段代码里无效。在网页元素查看器里看到返回的数值还是有注释符

你为什么要自作聪明呢

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


之前我不懂那个是xml里的注释,现在明白了,所以想到了另一种方法,可是还是失败了,能告诉我下原因吗?
	$xml = '<xml version="1.0" encoding="gbk"?>'.$xml;	var_dump(simplexml_load_string($xml));
Copy after login

我是用字符串运算符加了一行xml的文件标记,可是用simplexml_load_string的时候依然显示失败

还是那话,你为什么要自作聪明呢

你为什么要自作聪明呢


你给我的代码贴上去也是无法载入...

那是因为你不给我你的数据

还是那话,你为什么要自作聪明呢



这是直接复制给我的代码贴上去运行后的结果...

依然得去掉simplexml载入的语句才能dump出来

你光截图是没有用的!
你怎么知道其中没有不可打印的字符呢?

那是因为你不给我你的数据


<?xml version="1.0" encoding="gbk"?><xsm code="0000" msg="验证成功abc" trans_time="20140808162708"><userId>114</userId><nickName>wedc</nickName><userType>2</userType><comId>116</comId><saledptId>11601</saledptId><refId>1062014</refId><comName></comName><domainUrl>v=2014080600</domainUrl><comType>02</comType><comShort></comShort><parentComId>11620001</parentComId><expirationTime>1407488228735</expirationTime><planText>10116226011162288228735</planText><signatureValue>c6959b4eacf7b2f</signatureValue></xsm>
Copy after login

这是我用fwrite写入的$xml值

里面没有注释掉文件标记,可不知道问什么不能载入

你光截图是没有用的!
你怎么知道其中没有不可打印的字符呢?


在IE下查看会显示
结束标记 'xsm' 与开始标记 'comShort' 不匹配。

SimpleXMLElement Object(    [@attributes] => Array        (            [code] => 0000            [msg] => 验证成功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)
Copy after login
Copy after login
Copy after login
这不是可以吗?

你截图中显示字符串长度为 984 字节,而你贴出的只有 509 字节。
还有四百多字节到哪里去了?

你光截图是没有用的!
你怎么知道其中没有不可打印的字符呢?


版主,我知道,原因是里面有中文所以不能载入,这个如何才能解决?因为数据不是我自己的所以我也没法改

SimpleXMLElement Object(    [@attributes] => Array        (            [code] => 0000            [msg] => 验证成功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)
Copy after login
Copy after login
Copy after login
这不是可以吗?

你截图中显示字符串长度为 984 字节,而你贴出的只有 509 字节。
还有四百多字节到哪里去了?


还有400字节是一长串字符串太长我就删了半截
谢谢你,我发现原因了,是因为PHP用utf8编码的,而获取的xml数据时gbk的,把文件里的gbk改成utf8就解决了

SimpleXMLElement Object(    [@attributes] => Array        (            [code] => 0000            [msg] => 验证成功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)
Copy after login
Copy after login
Copy after login
这不是可以吗?

你截图中显示字符串长度为 984 字节,而你贴出的只有 509 字节。
还有四百多字节到哪里去了?
成功了 ~~~~~~[]

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

A deep dive into the meaning and usage of HTTP status code 460 A deep dive into the meaning and usage of HTTP status code 460 Feb 18, 2024 pm 08:29 PM

In-depth analysis of the role and application scenarios of HTTP status code 460 HTTP status code is a very important part of web development and is used to indicate the communication status between the client and the server. Among them, HTTP status code 460 is a relatively special status code. This article will deeply analyze its role and application scenarios. Definition of HTTP status code 460 The specific definition of HTTP status code 460 is "ClientClosedRequest", which means that the client closes the request. This status code is mainly used to indicate

iBatis and MyBatis: Comparison and Advantage Analysis iBatis and MyBatis: Comparison and Advantage Analysis Feb 18, 2024 pm 01:53 PM

iBatis and MyBatis: Differences and Advantages Analysis Introduction: In Java development, persistence is a common requirement, and iBatis and MyBatis are two widely used persistence frameworks. While they have many similarities, there are also some key differences and advantages. This article will provide readers with a more comprehensive understanding through a detailed analysis of the features, usage, and sample code of these two frameworks. 1. iBatis features: iBatis is an older persistence framework that uses SQL mapping files.

Detailed explanation of Oracle error 3114: How to solve it quickly Detailed explanation of Oracle error 3114: How to solve it quickly Mar 08, 2024 pm 02:42 PM

Detailed explanation of Oracle error 3114: How to solve it quickly, specific code examples are needed. During the development and management of Oracle database, we often encounter various errors, among which error 3114 is a relatively common problem. Error 3114 usually indicates a problem with the database connection, which may be caused by network failure, database service stop, or incorrect connection string settings. This article will explain in detail the cause of error 3114 and how to quickly solve this problem, and attach the specific code

Analysis of the meaning and usage of midpoint in PHP Analysis of the meaning and usage of midpoint in PHP Mar 27, 2024 pm 08:57 PM

[Analysis of the meaning and usage of midpoint in PHP] In PHP, midpoint (.) is a commonly used operator used to connect two strings or properties or methods of objects. In this article, we’ll take a deep dive into the meaning and usage of midpoints in PHP, illustrating them with concrete code examples. 1. Connect string midpoint operator. The most common usage in PHP is to connect two strings. By placing . between two strings, you can splice them together to form a new string. $string1=&qu

Parsing Wormhole NTT: an open framework for any Token Parsing Wormhole NTT: an open framework for any Token Mar 05, 2024 pm 12:46 PM

Wormhole is a leader in blockchain interoperability, focused on creating resilient, future-proof decentralized systems that prioritize ownership, control, and permissionless innovation. The foundation of this vision is a commitment to technical expertise, ethical principles, and community alignment to redefine the interoperability landscape with simplicity, clarity, and a broad suite of multi-chain solutions. With the rise of zero-knowledge proofs, scaling solutions, and feature-rich token standards, blockchains are becoming more powerful and interoperability is becoming increasingly important. In this innovative application environment, novel governance systems and practical capabilities bring unprecedented opportunities to assets across the network. Protocol builders are now grappling with how to operate in this emerging multi-chain

Analysis of new features of Win11: How to skip logging in to Microsoft account Analysis of new features of Win11: How to skip logging in to Microsoft account Mar 27, 2024 pm 05:24 PM

Analysis of new features of Win11: How to skip logging in to a Microsoft account. With the release of Windows 11, many users have found that it brings more convenience and new features. However, some users may not like having their system tied to a Microsoft account and wish to skip this step. This article will introduce some methods to help users skip logging in to a Microsoft account in Windows 11 and achieve a more private and autonomous experience. First, let’s understand why some users are reluctant to log in to their Microsoft account. On the one hand, some users worry that they

Analysis of exponential functions in C language and examples Analysis of exponential functions in C language and examples Feb 18, 2024 pm 03:51 PM

Detailed analysis and examples of exponential functions in C language Introduction: The exponential function is a common mathematical function, and there are corresponding exponential function library functions that can be used in C language. This article will analyze in detail the use of exponential functions in C language, including function prototypes, parameters, return values, etc.; and give specific code examples so that readers can better understand and use exponential functions. Text: The exponential function library function math.h in C language contains many functions related to exponentials, the most commonly used of which is the exp function. The prototype of exp function is as follows

See all articles