php - trouble parsing xml with simplexml -


i pulling client info remote server , creating xml file on website parsed webpage. using code:

$xml=simplexml_load_file("test.xml");     print_r($xml); 

outputs following:

simplexmlelement object ( [@attributes] => array ( [requesttype] => schema ) )  

here xml file maybe can see if there error or in output:

    <b1reply:response requesttype="schema">     <b1reply:uri>/query/accountbalance.aspx?custcardcodefrom=c1007&custcardcodeto=c1007</b1reply:uri>     <b1reply:status>ok</b1reply:status>     <b1reply:payload schemadigest="ecdf0140846bf818a8a0605cb609bb7e">     <querydata queryname="/query//accountbalance.aspx?custcardcodefrom=c1007&custcardcodeto=c1007">     <columns>     <column name="id" alias="id" type="int64" key="false"/>     <column name="companyname" alias="companyname" type="string" key="false"/>     <column name="companyaddr" alias="companyaddr" type="string" key="false"/>     <column name="phone" alias="phone" type="string" key="false"/>     <column name="fax" alias="fax" type="string" key="false"/>     <column name="companycurrency" alias="companycurrency" type="string" key="false"/>     <column name="_custstatmenthdr_cardcode" alias="cardcode" type="string" key="false"/>     <column name="_custstatmenthdr_cardname" alias="cardname" type="string" key="false"/>  <column name="_custstatmenthdr_cardtype" alias="cardtype" type="string" key="false"/> <column name="_custstatmenthdr_balance" alias="balance" type="decimal" key="false"/> <column name="_custstatmenthdr_balancefc" alias="balancefc" type="decimal" key="false"/> <column name="_custstatmenthdr_balancesys" alias="balancesys" type="decimal" key="false"/> <column name="_custstatmenthdr_currency" alias="currency" type="string" key="false"/> <column name="_custstatmenthdr_pymntgroup" alias="pymntgroup" type="string" key="false"/> <column name="_custstatmenthdr_groupname" alias="groupname" type="string" key="false"/> <column name="_custstatmenthdr_slpname" alias="slpname" type="string" key="false"/> <column name="_custstatmentdtl_transid" alias="transid" type="int32" key="false"/> <column name="_custstatmentdtl_transtype" alias="transtype" type="int32" key="false"/> <column name="_custstatmentdtl_dtlcardcode" alias="dtlcardcode" type="string" key="false"/> <column name="_custstatmentdtl_refdate" alias="refdate" type="datetime" key="false"/> <column name="_custstatmentdtl_taxdate" alias="taxdate" type="datetime" key="false"/> <column name="_custstatmentdtl_duedate" alias="duedate" type="datetime" key="false"/> <column name="_custstatmentdtl_ref1" alias="ref1" type="string" key="false"/> <column name="_custstatmentdtl_amount" alias="amount" type="decimal" key="false"/> <column name="_custstatmentdtl_syscred" alias="syscred" type="decimal" key="false"/> <column name="_custstatmentdtl_sysdeb" alias="sysdeb" type="decimal" key="false"/> <column name="_custstatmentdtl_sourceline" alias="sourceline" type="int16" key="false"/> <column name="_custstatmentdtl_document_id" alias="document id" type="string" key="false"/> <column name="_custstatmentdtl_customer_po_no." alias="customer po no." type="string" key="false"/> <column name="_custstatmentdtl_source_document_id" alias="source document id" type="int32" key="false"/> <column name="_custstatmentdtl_trantype" alias="trantype" type="string" key="false"/> <column name="_custstatmentdtl_docamount" alias="docamount" type="decimal" key="false"/> <column name="_custstatmentdtl_journalline" alias="journalline" type="int16" key="false"/> </columns> <data> <row id="1"> <id>2</id> <companyname/> <companyaddr>address</companyaddr> <phone>555-555-5555</phone> <fax>555-555-5555</fax> <companycurrency>$</companycurrency> <_custstatmenthdr_cardcode>c1007</_custstatmenthdr_cardcode> <_custstatmenthdr_cardname>card name</_custstatmenthdr_cardname> <_custstatmenthdr_cardtype>c</_custstatmenthdr_cardtype> <_custstatmenthdr_balance>168069.405000</_custstatmenthdr_balance> <_custstatmenthdr_balancefc>0.000000</_custstatmenthdr_balancefc> <_custstatmenthdr_balancesys>168069.405000</_custstatmenthdr_balancesys> <_custstatmenthdr_currency>$</_custstatmenthdr_currency> <_custstatmenthdr_pymntgroup>net 7 days</_custstatmenthdr_pymntgroup> <_custstatmenthdr_groupname>food distribution</_custstatmenthdr_groupname> <_custstatmenthdr_slpname>john doe</_custstatmenthdr_slpname> <_custstatmentdtl_transid>5036</_custstatmentdtl_transid> <_custstatmentdtl_transtype>13</_custstatmentdtl_transtype> <_custstatmentdtl_dtlcardcode>c1007</_custstatmentdtl_dtlcardcode> <_custstatmentdtl_refdate>7/5/2013 12:00:00 am</_custstatmentdtl_refdate> <_custstatmentdtl_taxdate>7/5/2013 12:00:00 am</_custstatmentdtl_taxdate> <_custstatmentdtl_duedate>7/12/2013 12:00:00 am</_custstatmentdtl_duedate> <_custstatmentdtl_ref1>106346</_custstatmentdtl_ref1> <_custstatmentdtl_amount>27709.610000</_custstatmentdtl_amount> <_custstatmentdtl_syscred>0.000000</_custstatmentdtl_syscred> <_custstatmentdtl_sysdeb>27709.610000</_custstatmentdtl_sysdeb> <_custstatmentdtl_sourceline>1</_custstatmentdtl_sourceline> <_custstatmentdtl_document_id>106346</_custstatmentdtl_document_id> <_custstatmentdtl_customer_po_no.>469</_custstatmentdtl_customer_po_no.> <_custstatmentdtl_source_document_id>1347</_custstatmentdtl_source_document_id> <_custstatmentdtl_trantype>invoice</_custstatmentdtl_trantype> <_custstatmentdtl_docamount>27709.610000</_custstatmentdtl_docamount> <_custstatmentdtl_journalline>0</_custstatmentdtl_journalline> </row> </data> </querydata> </b1reply:payload> <b1reply:error/> </b1reply:response> 

if try echo single attribute, returns blank. i'm @ loss, , noob @ simplexml. appreciated.

first, xml not valid, see http://www.xmlvalidation.com

to select attributes of node, use xpath, here specific attribute specific node:

$xml = simplexml_load_string($x); // assume xml in $x $result = (string)$xml->xpath("//column[@name = '_custstatmenthdr_currency']/@alias")[0]; echo $result; 

output:

currency 

comments:

(1) code takes php >= 5.4 array dereferencing [0] in line 2
(2) cast (string), otherwise simplexml-object back

to column nodes attributes, do:

$results = $xml->xpath("//column"); foreach ($results $result) {     foreach ($result->attributes() $name, $value)         echo "$name: $value, ";     echo php_eol; }       

see working: https://eval.in/40273


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

android - Inheriting from Theme.AppCompat* -