简体   繁体   English

如何使用XPath PHP获取XML元素

[英]How to get element xml using xpath php

I have issue with how to get element xml using xpath php, i already create a php file to extract the "attributes" xml by using xpath php. 我在使用xpath php获取元素xml时遇到问题,我已经创建了一个php文件,以通过使用xpath php提取“属性” xml。

What i want is how to extract every element in xml by using xpath. 我想要的是如何使用xpath提取xml中的每个元素。

test.xml 的test.xml

<?xml version="1.0" encoding="UTF-8"?>
<InvoicingData>
 <CreationDate> 2014-02-02 </CreationDate>
 <OrderNumber> XXXX123 </OrderNumber>
 <InvoiceDetails>
   <InvoiceDetail>
   <SalesCode> XX1A </SalesCode>
   <SalesName> JohnDoe </SalesName>
</InvoiceDetail>
</InvoiceDetails>
</InvoicingData>

read.php read.php

<?php
$doc = new DOMDocument();
$doc->loadXML(file_get_contents("test.xml"));
$xpath = new DOMXpath($doc);
$nodes = $xpath->query('//*');
$names = array();
foreach ($nodes as $node)
{
    $names[] = $node->nodeName;
}
echo join(PHP_EOL, ($names));
?>

From the code above it will print like this : 从上面的代码中,它将像这样打印:

CreationDate OrderNumber InvoiceDetails InvoiceDetail SalesCode SalesName CreationDate订单号InvoiceDetails InvoiceDetail SalesCode SalesName

So, the problem is, how to get the element inside the attribute, basically this is what i want to print : 所以,问题是,如何获取属性内的元素, 基本上这就是我要打印的内容

2014-02-02 XXXX123 XX1A JohnDoe 2014-02-02 XXXX123 XX1A JohnDoe

You use $node->textContent to get the textual value of the node (and its descendants, if any). 您可以使用$node->textContent来获取节点(及其后代,如果有)的文本值。


In response to your first comment: 针对您的第一条评论:

You didn't use $node->textContent . 您没有使用$node->textContent Try this: 尝试这个:

$doc = new DOMDocument();
$doc->loadXML(file_get_contents("test.xml"));
$xpath = new DOMXpath($doc);
$nodes = $xpath->query('//*');
$names = array();
$values = array(); // created a separate array for the values
foreach ($nodes as $node)
{
  $names[]  = $node->nodeName;
  $values[] = $node->textContent; // push to $values array
}
echo join(PHP_EOL, ($values));

However, if you only want to push the textual values when they're a direct child of an element and still want to collect all node names as well, you could do something like: 但是,如果只希望在文本值是元素的直接子代时推送文本值,并且仍然希望收集所有节点名,则可以执行以下操作:

foreach ($nodes as $node)
{
  $names[] = $node->nodeName;
  // check that this node only contains one text node
  if( $node->childNodes->length == 1 && $node->firstChild instanceof DOMText ) {
    $values[] = $node->textContent;
  }
}
echo join(PHP_EOL, ($values));

And if you only care about the nodes that directly contain textual values, you could do something like this: 而且,如果您只关心直接包含文本值的节点,则可以执行以下操作:

// this XPath query only selects those nodes that directly contain non-whitespace text
$nodes = $xpath->query('//*[./text()[normalize-space()]]');
$values = array();
foreach ($nodes as $node)
{
  // add nodeName as key
  // (only works reliable of there's never a duplicate nodeName in your XML)
  // and add textContent as value
  $values[ $node->nodeName ] = trim( $node->textContent );
}

var_dump( $values );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM