使用 simplexml_load_string 解析 XML 时缺少属性
Missing Attributes while parsing XML with simplexml_load_string
$xml = simplexml_load_string($value);
$json = json_encode($xml); // convert the XML string to JSON
$array = json_decode($json,TRUE);
转换为数组后缺少属性。
你所说的 <SampleData>
值是经过编码的,将其返回 'normal' 的最简单方法是使用 htmlspecialchars_decode()
在将字符串加载到 SimpleXML
。下面的代码执行此操作,然后输出数据的各个部分作为如何显示信息的示例...
$source = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=biosample&id=367368";
$value = file_get_contents($source);
$value = htmlspecialchars_decode($value);
$xml = simplexml_load_string($value);
// Access the DbBuild value
echo "DbBuild=".(string)$xml->DocumentSummarySet->DbBuild.PHP_EOL;
// The BioSample publication date attribute
echo "BioSample publication date=".(string)$xml->DocumentSummarySet->DocumentSummary->SampleData->BioSample['publication_date'].PHP_EOL;
// List the attributes name and value
foreach ( $xml->DocumentSummarySet->DocumentSummary->SampleData->BioSample->Attributes->Attribute as $attribute ) {
echo (string)$attribute['attribute_name']."=".(string)$attribute.PHP_EOL;
}
有些XML访问看起来很啰嗦,但这只是访问文档中各种级别数据的情况。 $xml->DocumentSummarySet
访问根元素下的 <DocumentSummarySet>
元素。 BioSample['publication_date']
是 <BioSample>
元素中的 publication_date 属性,依此类推。
有一个非常简单的解决方案 - 删除这两行代码:
$json = json_encode($xml); // convert the XML string to JSON
$array = json_decode($json,TRUE);
XML、JSON和PHP数组对于可以表示什么样的结构都有不同的规则,因此在它们之间任意转换将总是 最终会遇到丢失数据的边缘情况。 SimpleXML,顾名思义,旨在简单易用,因此您实际使用它会更好:
$xml = simplexml_load_string($value);
// Now access your data from $xml; no further conversion is needed
由于您没有提供有关您的 XML 长什么样的更多信息,因此我无法提供有关如何处理它的任何进一步信息,但有 extensive examples in the PHP manual, and .
$xml = simplexml_load_string($value);
$json = json_encode($xml); // convert the XML string to JSON
$array = json_decode($json,TRUE);
转换为数组后缺少属性。
你所说的 <SampleData>
值是经过编码的,将其返回 'normal' 的最简单方法是使用 htmlspecialchars_decode()
在将字符串加载到 SimpleXML
。下面的代码执行此操作,然后输出数据的各个部分作为如何显示信息的示例...
$source = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=biosample&id=367368";
$value = file_get_contents($source);
$value = htmlspecialchars_decode($value);
$xml = simplexml_load_string($value);
// Access the DbBuild value
echo "DbBuild=".(string)$xml->DocumentSummarySet->DbBuild.PHP_EOL;
// The BioSample publication date attribute
echo "BioSample publication date=".(string)$xml->DocumentSummarySet->DocumentSummary->SampleData->BioSample['publication_date'].PHP_EOL;
// List the attributes name and value
foreach ( $xml->DocumentSummarySet->DocumentSummary->SampleData->BioSample->Attributes->Attribute as $attribute ) {
echo (string)$attribute['attribute_name']."=".(string)$attribute.PHP_EOL;
}
有些XML访问看起来很啰嗦,但这只是访问文档中各种级别数据的情况。 $xml->DocumentSummarySet
访问根元素下的 <DocumentSummarySet>
元素。 BioSample['publication_date']
是 <BioSample>
元素中的 publication_date 属性,依此类推。
有一个非常简单的解决方案 - 删除这两行代码:
$json = json_encode($xml); // convert the XML string to JSON
$array = json_decode($json,TRUE);
XML、JSON和PHP数组对于可以表示什么样的结构都有不同的规则,因此在它们之间任意转换将总是 最终会遇到丢失数据的边缘情况。 SimpleXML,顾名思义,旨在简单易用,因此您实际使用它会更好:
$xml = simplexml_load_string($value);
// Now access your data from $xml; no further conversion is needed
由于您没有提供有关您的 XML 长什么样的更多信息,因此我无法提供有关如何处理它的任何进一步信息,但有 extensive examples in the PHP manual, and