2014-02-20 134 views
0

我从xml中获取值时遇到了一些问题。PHP - 从xml中获取值

XML看起来像

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="http://crd.gov.pl/xml/schematy/UPO/2008/05/09/UPO.xsl"?> 
<pos:Document xmlns:pos="SOMEURL" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <pos:DescribeDoc/> 
    <pos:UPD> 
     <pos:IdDoc>procotol-UPD2198338</pos:IdDoc> 
     <pos:IdCases>221872</pos:IdCases> 
     <pos:additionalInfo TypeInfo="Source">Some string</pos:additionalInfo> 
    </pos:UPD> 
... 

我一般尽量去POS:IdCases。 我试试这个代码:

$domContent = new SimpleXMLElement(((string) $content), LIBXML_COMPACT); 
$test = $domContent->xpath('/pos:Document/pos:UPD/*'); 
foreach($test as $node){ 
    print_r($node) 
} 

,我收到了一些对象,如

SimpleXMLElement Object 
(
    [0] => procotol-UPD2198338 
) 

SimpleXMLElement Object 
(
    [0] => 221872 
) 

SimpleXMLElement Object 
(
    [@attributes] => Array 
     (
      [TypeInfo] => Source 
     ) 

    [0] => Some string 
) 

但我一定要到POS:IdCases。我无法使用索引[1],因为订单可能会更改。

我的问题是: 我要如何才能到达值节点:POS:IdCases 我不能添加ID或其他信息节点,因为这个XML签署(XAdES的)。

你能给我一些建议吗?感谢您的帮助

回答

2

简单地改变XPath来匹配<Pos:IdCases/>节点:

$test = $domContent->xpath('/pos:Document/pos:UPD/pos:IdCases'); 
+0

....这是绝对明显。谢谢 –