Hi guys I am trying to pharse an xml file in php using simplexmlelement.
嗨伙计们,我正在尝试使用simplexmlelement在php中解析xml文件。
<OrderResponse>
<OrderResponseReferences>
<BuyersOrderNumber Preserve="true">100000002</BuyersOrderNumber>
<SuppliersOrderReference Preserve="true">6711637</SuppliersOrderReference>
</OrderResponseReferences>
</OrderResponse>
I am using the code below to access BuyersOrderNumber
我使用下面的代码访问BuyersOrderNumber
$local_file_path = "C:/etrade_files/Orders/order_acknowledgements/ORA1707220-05-2015--16-48-31.xml";
$xml = simplexml_load_file($local_file_path);
foreach($xml->children() as $child)
{
$child = $child->getName();
if($child == "OrderResponseReferences")
{
$order_reference = $xml->$child->BuyersOrderNumber;
print_r($order_reference);
}
}
Result:
结果:
SimpleXMLElement Object ( [@attributes] => Array ( [Preserve] => true ) [0] => 100000002 )
How can I just get the order number which is 100000002
我怎样才能获得100000002的订单号
Also this files have multiple order lines it would really help me if someone can explain the logic I can use to get all the required information.
此文件还有多个订单行,如果有人可以解释我可以使用的逻辑来获取所有必需的信息,它将真正帮助我。
<OrderResponseLine>
<LineNumber>1</LineNumber>
<OrderResponseLineReferences>
<BuyersOrderLineReference Preserve="true">100000002</BuyersOrderLineReference>
</OrderResponseLineReferences>
<Product>
<SuppliersProductCode>108696</SuppliersProductCode>
</Product>
<Quantity>
<Amount>2</Amount>
</Quantity>
<LineTotal>331.18</LineTotal>
</OrderResponseLine>
It is my first time working with xml I would really appreciate if someone could help. Thank you.
这是我第一次使用xml我真的很感激,如果有人可以提供帮助。谢谢。
4 个解决方案
#1
0
If you cast the object to a string, it'll use its value and convert that:
如果将对象强制转换为字符串,它将使用其值并转换为:
print_r((string) $order_reference);
// 100000002
I tried your code and you're doing well! Just the (string)
should be enough to get the script running.
我尝试了你的代码,你做得很好!只需(字符串)就足以让脚本运行了。
#2
0
I would recommend you to use DOM in order to parse this XML.
我建议你使用DOM来解析这个XML。
$xml = new DOMDocument();
$xml->load('ORA1707220-05-2015--16-48-31.xml');
$root = $xml->getElementsByTagName('OrderResponse');
$tree = $root->item(0);
$child = $tree->childNodes;
foreach($child as $children){
switch($children->nodeName){
case 'OrderResponseReferences':
//instructions
}
}
And use nodeValue
to get value of the node.
并使用nodeValue获取节点的值。
#3
0
DOMDocument and recursive function seems me working to get this value.
DOMDocument和递归函数似乎我正在努力获得这个值。
$xml = new DOMDocument();
$xml->load('ORA1707220-05-2015--16-48-31.xml');
$root = $xml->getElementsByTagName('OrderResponse');
$tree = $root->item(0);
parse($tree);
function parse($tree){
$child = $tree->childNodes;
foreach($child as $children){
switch($children->nodeName){
//Case
//instructions
}
if($child->hasChildNodes())
parse($child);
}
}
If there's subnodes in the current node, it will call parse() again on the child, and you'll get the value yu want
如果当前节点中有子节点,它将再次调用子节点上的parse(),你将得到yu想要的值
#4
0
There are much easier ways:
有更简单的方法:
retrieve many <BuyersOrderNumber>
:
检索许多
use xpath
:
使用xpath:
$numbers = $xml->xpath("//BuyersOrderNumber");
Explanation:
说明:
- the
xpath
statement will select any<BuyersOrderNumber>
-
xpath语句将选择任何
- anywhere in the XML tree (due to the
//
in front of the node name) - XML树中的任何位置(由于节点名称前面的//)
-
$numbers
will hold an array ofSimpleXML
elements - $ numbers将包含一系列SimpleXML元素
Output all nodes:
输出所有节点:
foreach ($numbers as $n)
echo $n;
retrieve one single <BuyersOrderNumber>
node
检索一个
-
the standard way: if there is only one such node in the XML:
标准方式:如果XML中只有一个这样的节点:
echo $xml->OrderResponseReferences->BuyersOrderNumber;
-
again
xpath
:再次xpath:
echo $xml->xpath("//BuyersOrderNumber")[0];
get all nodes and echo the first one only (that's why the
[0]
)获取所有节点并仅回显第一个节点(这就是为什么[0])
see a working example: https://eval.in/368739
看一个有效的例子:https://eval.in/368739
Of course, $xml
in the examples is..
当然,示例中的$ xml是..
$xml = simplexml_load_file($local_file_path);
Reference: http://php.net/manual/en/simplexml.examples-basic.php
参考:http://php.net/manual/en/simplexml.examples-basic.php
#1
0
If you cast the object to a string, it'll use its value and convert that:
如果将对象强制转换为字符串,它将使用其值并转换为:
print_r((string) $order_reference);
// 100000002
I tried your code and you're doing well! Just the (string)
should be enough to get the script running.
我尝试了你的代码,你做得很好!只需(字符串)就足以让脚本运行了。
#2
0
I would recommend you to use DOM in order to parse this XML.
我建议你使用DOM来解析这个XML。
$xml = new DOMDocument();
$xml->load('ORA1707220-05-2015--16-48-31.xml');
$root = $xml->getElementsByTagName('OrderResponse');
$tree = $root->item(0);
$child = $tree->childNodes;
foreach($child as $children){
switch($children->nodeName){
case 'OrderResponseReferences':
//instructions
}
}
And use nodeValue
to get value of the node.
并使用nodeValue获取节点的值。
#3
0
DOMDocument and recursive function seems me working to get this value.
DOMDocument和递归函数似乎我正在努力获得这个值。
$xml = new DOMDocument();
$xml->load('ORA1707220-05-2015--16-48-31.xml');
$root = $xml->getElementsByTagName('OrderResponse');
$tree = $root->item(0);
parse($tree);
function parse($tree){
$child = $tree->childNodes;
foreach($child as $children){
switch($children->nodeName){
//Case
//instructions
}
if($child->hasChildNodes())
parse($child);
}
}
If there's subnodes in the current node, it will call parse() again on the child, and you'll get the value yu want
如果当前节点中有子节点,它将再次调用子节点上的parse(),你将得到yu想要的值
#4
0
There are much easier ways:
有更简单的方法:
retrieve many <BuyersOrderNumber>
:
检索许多
use xpath
:
使用xpath:
$numbers = $xml->xpath("//BuyersOrderNumber");
Explanation:
说明:
- the
xpath
statement will select any<BuyersOrderNumber>
-
xpath语句将选择任何
- anywhere in the XML tree (due to the
//
in front of the node name) - XML树中的任何位置(由于节点名称前面的//)
-
$numbers
will hold an array ofSimpleXML
elements - $ numbers将包含一系列SimpleXML元素
Output all nodes:
输出所有节点:
foreach ($numbers as $n)
echo $n;
retrieve one single <BuyersOrderNumber>
node
检索一个
-
the standard way: if there is only one such node in the XML:
标准方式:如果XML中只有一个这样的节点:
echo $xml->OrderResponseReferences->BuyersOrderNumber;
-
again
xpath
:再次xpath:
echo $xml->xpath("//BuyersOrderNumber")[0];
get all nodes and echo the first one only (that's why the
[0]
)获取所有节点并仅回显第一个节点(这就是为什么[0])
see a working example: https://eval.in/368739
看一个有效的例子:https://eval.in/368739
Of course, $xml
in the examples is..
当然,示例中的$ xml是..
$xml = simplexml_load_file($local_file_path);
Reference: http://php.net/manual/en/simplexml.examples-basic.php
参考:http://php.net/manual/en/simplexml.examples-basic.php