php/ mysql/ xml/ dom

I have a code :

$query = '//Document/Status[. = "Posted"]';
$names = $xpath->query($query); // A list of matched elements

result

 <Document> <Company>SEC</Company> <Type description="Misc Customer Invoices">CINV</Type> <DocumentID>CI_INV000239562</DocumentID> <DocumentDescription>Invoice</DocumentDescription> <Status>Posted</Status> <Owner>42570935</Owner> <Customer>42570935</Customer> <Location>42570935</Location> <Date>20111115</Date> <DueDate>20111115</DueDate> <OpenDate>20111115</OpenDate> <RequiredDate>20111115</RequiredDate> <TaxCode>AUS GST</TaxCode> <TaxType>Incl</TaxType> <TaxRate>10.00</TaxRate> <CurrencyCode>AUD</CurrencyCode> <SubTotal>115.00</SubTotal> <TotalTax>11.50</TotalTax> <Total>126.50</Total> <AmountDue>126.50</AmountDue> <AmountPaid>126.50</AmountPaid> </Document> 

If I want to display

  <DocumentID>CI_INV000239562</DocumentID> 

only

What should I do ?

If I want more node to be displayed ? for example AmmountDue ?

$query = '//Document/Status[. = "Posted"]/../DocumentID/AmmountDue';

Is this possible or I must use another method ?

Thanks

There are few ways to do this, here is one of the example :-

$query = '//Document/Status[. = "Posted"]/../DocumentID';
...
$names = $xpath->query($query);
foreach ($names as $name)
{
  $node = $name->nodeValue; // string of "CI_INV000239562"
}

To seek for multiple node name :-

//Document/Status[. = "Posted"]/../*[self::DocumentID or self::AmountDue]

$query = '//Document/Status[. = "Posted"]/DocumentID';

暂无
暂无

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question How to parse only one option of XML in PHP? Read One Node of XML in PHP output only a specific node PHP XML Editing one XML node value in PHP How to print xml node in php how to process an XML NODE in PHP PHP - PDO - How to fetch multiple rows with only one query? Loop for only one category XML-PHP select only one node and move on php Php query only performing one time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM