PHP从xml文件中获取特定数据

PHP从xml文件中获取特定数据,php,xml,Php,Xml,这是一个XML文件 <?xml version="1.0" encoding="utf-8"?> <UW xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd"> <UWdata> <List> <

这是一个XML文件

<?xml version="1.0" encoding="utf-8"?>
<UW xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">
  <UWdata>
    <List>
      <IdProduct>1</IdProduct>
      <ProductName>product</ProductName>
      <ProductNameDE>product</ProductNameDE>
      <ProductNameEN>product</ProductNameEN>
      <Uf>1</Uf>
      <PSIg>1</PSIg>
      <Ug>1</Ug>
    </List>
  </UWdata>
</UW> 


$lines_array=file($url);
$lines_string=implode('',$lines_array);
$xml=simplexml_load_string($lines_string) or die("Error: Cannot create object");

但是它不返回任何内容。我想返回产品名称。

我修改了您的脚本,并按原样将xml放入名为
testxml.xml的外部文件中。始终将函数和它应该处理的数据分开。我像这样使用您的xml:

<?xml version="1.0" encoding="utf-8"?>
<UW xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">
  <UWdata>
    <List>
      <IdProduct>1</IdProduct>
      <ProductName>productTEST</ProductName>
      <ProductNameDE>product</ProductNameDE>
      <ProductNameEN>product</ProductNameEN>
      <Uf>1</Uf>
      <PSIg>1</PSIg>
      <Ug>1</Ug>
    </List>
  </UWdata>
</UW>
希望这有帮助

//编辑:
虽然我不知道您的项目,但如果您的xml可能包含多个
列表
元素示例代码,您可能需要采用foreach方法,使用
simplexml\u load\u string

<?php
$a = '<?xml version="1.0" encoding="utf-8"?>
<UW xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">
  <UWdata>
    <List>
      <IdProduct>1</IdProduct>
      <ProductName>product</ProductName>
      <ProductNameDE>product</ProductNameDE>
      <ProductNameEN>product</ProductNameEN>
      <Uf>1</Uf>
      <PSIg>1</PSIg>
      <Ug>1</Ug>
    </List>
  </UWdata>
</UW>';

$xml=simplexml_load_string($a) or die("Error: Cannot create object");

echo ($xml->UWdata->List->ProductName);
?>
UWdata->List->ProductName);
?>

print\r($xml)的任何结果?然后在读取
simplexml\u load\u字符串时尝试“simplexml\u load\u file”
您的索引应该是
[0]
,而您错过了对象中的
->列表。您还需要强制转换为字符串:
echo(string)$xml->UWdata[0]->List->ProductName
我遇到错误:尝试使用simplexml\u load\u文件,然后echo$xml->UWdata[1]->ProductName时无法创建对象;当我尝试echo$xml->UW[0]->UWdata[0]->ProductName时也是如此;它什么也不返回。@BiljanaGlumac你试过我的建议了吗?它在我的控制台上没有帮助,它什么也不返回。调用php文件可能有问题。我这样叫。$。ajax({url:“/wp admin/uw_calulation.php”,键入:“GET”,success:function(data){console.log(data);});当我通过您的代码并运行时,它确实会返回数据,但当我使用url调用file_get_contents并对从url返回的xml执行此操作时,它不会返回任何内容。
$xmlstr = file_get_contents('./testxml.xml');
$xml = simplexml_load_string($xmlstr);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
echo $array['UWdata']['List']['ProductName'];
<?php
$a = '<?xml version="1.0" encoding="utf-8"?>
<UW xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">
  <UWdata>
    <List>
      <IdProduct>1</IdProduct>
      <ProductName>product</ProductName>
      <ProductNameDE>product</ProductNameDE>
      <ProductNameEN>product</ProductNameEN>
      <Uf>1</Uf>
      <PSIg>1</PSIg>
      <Ug>1</Ug>
    </List>
  </UWdata>
</UW>';

$xml=simplexml_load_string($a) or die("Error: Cannot create object");

echo ($xml->UWdata->List->ProductName);
?>