Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP xml中的字段段后查找值段_Php_Xml - Fatal编程技术网

在PHP xml中的字段段后查找值段

在PHP xml中的字段段后查找值段,php,xml,Php,Xml,我需要使用PHP获得字段段后面的值段的值1250,值为瓶号。下面是xml的一个片段 <CustomerRecordExtraInfo> <Field>1</Field> <Value>1</Value> <DateSaved>2013-11-15T12:21:35</DateSaved> <SourceType /> <SourceID

我需要使用PHP获得字段段后面的值段的值1250,值为瓶号。下面是xml的一个片段

<CustomerRecordExtraInfo>
      <Field>1</Field>
      <Value>1</Value>
      <DateSaved>2013-11-15T12:21:35</DateSaved>
      <SourceType />
      <SourceID />
    </CustomerRecordExtraInfo>
    <CustomerRecordExtraInfo>
      <Field>Bottle Number</Field>
      <Value>1250</Value>
      <DateSaved>2013-12-11T15:22:34</DateSaved>
      <SourceType />
      <SourceID />
    </CustomerRecordExtraInfo>
    <CustomerRecordExtraInfo>
      <Field>City</Field>
      <Value>Heath</Value>
      <DateSaved>2013-12-11T15:22:34</DateSaved>
      <SourceType />
      <SourceID />
    </CustomerRecordExtraInfo>
你可以用语法分析它

我和你们分享两种方法,第一种是索引,第二种是迭代

在解析XML之前,您应该将其封装在根元素中(任何名称),我也添加了XML信息版本

<?php
$input = <<<XML
<?xml version='1.0' standalone='yes'?>
<root>
<CustomerRecordExtraInfo>
 <Field>1</Field>
 <Value>1</Value>
 <DateSaved>2013-11-15T12:21:35</DateSaved>
 <SourceType />
 <SourceID />
</CustomerRecordExtraInfo>
<CustomerRecordExtraInfo>
 <Field>Bottle Number</Field>
 <Value>1250</Value>
 <DateSaved>2013-12-11T15:22:34</DateSaved>
 <SourceType />
 <SourceID />
</CustomerRecordExtraInfo>
<CustomerRecordExtraInfo>
 <Field>City</Field>
 <Value>Heath</Value>
 <DateSaved>2013-12-11T15:22:34</DateSaved>
 <SourceType />
 <SourceID />
</CustomerRecordExtraInfo>
</root>
XML;
$xml = new SimpleXMLElement($input);

// Approach 1 - index
echo $xml->CustomerRecordExtraInfo[1]->Value;

// Approach 2 - iterative
foreach($xml->CustomerRecordExtraInfo as $customer) {
  if($customer->Field == "Bottle Number") {
    echo $customer->Value;
    break;
  }
}
?>
使用xpath,一种XML的“查询语言”:

// $xml holds the simplexml object
$result = $xml->xpath("//CustomerRecordExtraInfo[Field = 'Bottle Number']");
echo $result[0]->Value;

查看它的工作情况:

Xpath允许您使用表达式从XML提取数据。evaluate是PHP中使用它最灵活的函数

$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXpath($dom);

var_dump(
  $xpath->evaluate('string(//CustomerRecordExtraInfo[Field = "Bottle Number"]/Value)')
);
获取文档中任意位置的所有CustomerRecordExtraInfo元素:

//CustomerRecordExtranfo

仅当具有文本内容瓶编号的子元素字段时

//CustomerRecordExtranfo[字段=瓶号]

获取找到的节点的子元素值

//CustomerRecordExtranfo[字段=瓶号]/Value

并将其转换为字符串编号也是可能的

string//CustomerRecordExtraInfo[Field=瓶号]/Value

我只需通过谷歌搜索PHP XML就可以获得很多点击率。无论如何,看看