angelleye Posted June 3, 2007 Share Posted June 3, 2007 I will PayPal you $50 if you answer this for me!!! I'm having a heck of a time doing what I want with this XML document: http://angelleye.com/phpTraining/XML_Parsing_Samples/fmresultset.xml I think the primary problem is that I'm not understanding OOP as well as I need to. With that XML that I provided I've setup the following PHP script: <?php ini_set('display_errors', 1); error_reporting(E_ALL); $xmlDom = new DOMDocument(); $xmlDom -> load("fmresultset.xml"); $xmlDomXPath = new DOMXPath($xmlDom); $records = $xmlDomXPath -> query("/fmresultset/resultset/record"); $recordCount = $records -> length; foreach($records as $record) { //for each <record> returned... $fields = $record -> getElementsByTagName("field"); foreach($fields as $field) { //for each <field> in the <record> parse out all data needed. $fieldName = $field -> getAttribute("name"); if($fieldName == "Ref_Number") $Ref_Number = $field -> nodeValue; elseif($fieldName == "Qty_Available") $Qty_Available = $field -> nodeValue; elseif($fieldName == "Purchase_Order_Items::Condition") $Condition = $field -> nodeValue; } //now that i have my values stored i can move on and do //what i need with them for this current <record> echo "RefNumber: " . $Ref_Number . "<br /> Qty. Available: " . $Qty_Available . "<br /> Condition: " . $Condition . "<br /><br />"; } ?> That drives me crazy that it has to loop through every <field> like that and check the value of the attribute to see if it's the correct one or not in order to store the value into a variable. If I could make use of the DOMXPath methods inside of the foreach() loop then I could do what I want. I don't understand why the following doesn't work: <?php ini_set('display_errors', 1); error_reporting(E_ALL); $xmlDom = new DOMDocument(); $xmlDom -> load("fmresultset.xml"); $xmlDomXPath = new DOMXPath($xmlDom); $records = $xmlDomXPath -> query("/fmresultset/resultset/record"); $recordCount = $records -> length; foreach($records as $record) { //for each <record> returned... $Ref_Number = $record -> evaluate("/field[@name='Ref_Number']/data"); $Qty_Available = $record -> evaluate("/field[@name='Qty_Available']/data"); $Condition = $record -> evaluate("/field[@name='Qty_Available']/data"); //now that i have my values stored i can move on and do //what i need with them for this current <record> echo "RefNumber: " . $Ref_Number . "<br /> Qty. Available: " . $Qty_Available . "<br /> Condition: " . $Condition . "<br /><br />"; } ?> When I do that I get the error: Call to undefined method DOMElement::evaluate() evaluate() is a DOMXPath method that "Evaluates the given XPath expression and returns a typed result if possible" according to php.net documentation. The only methods I can get to work within the foreach() loop are DOMDocument methods the way I used getElementsByTagName(). That grabs ALL <field> elements, though, and then I have to loop through them all to check the attribute value's like I've done in that first code sample. This is not very efficient code. There's gotta be a way that I use XPath queries on each $record within the foreach() loop to grab exactly the value I'm looking for. I've been trying to figure it out for 3 weeks, though, with no luck. Any information I can get on how to accomplish this would be greatly appreciated. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/54080-solved-need-help-understanding-oop-in-this-simple-suppose-to-be-script-using-dom/ Share on other sites More sharing options...
angelleye Posted June 3, 2007 Author Share Posted June 3, 2007 Question has been answered!!! <?php ini_set('display_errors', 1); error_reporting(E_ALL); $xmlDom = new DOMDocument(); $xmlDom -> load("fmresultset.xml"); $xmlDomXPath = new DOMXPath($xmlDom); $records = $xmlDomXPath -> query("/fmresultset/resultset/record"); $recordCount = $records -> length; foreach($records as $record) { //for each <record> returned... $Ref_Number = $xmlDomXPath -> evaluate("field[@name='Ref_Number']/data", $record); $Qty_Available = $xmlDomXPath -> evaluate("field[@name='Qty_Available']/data", $record); $Condition = $xmlDomXPath -> evaluate("field[@name='Purchase_Order_Items::Condition']/data", $record); //now that i have my values stored i can move on and do //what i need with them for this current <record> echo "RefNumber: " . $Ref_Number->item(0)->nodeValue . "<br /> Qty. Available: " . $Qty_Available->item(0)->nodeValue . "<br /> Condition: " . $Condition->item(0)->nodeValue . "<br /><br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/54080-solved-need-help-understanding-oop-in-this-simple-suppose-to-be-script-using-dom/#findComment-267343 Share on other sites More sharing options...
mannbaig Posted October 8, 2007 Share Posted October 8, 2007 'My hosting at linux based server' I have this error "domdocument() [function.domdocument]: Start tag expected, '<' not found in /server/myfile.php on line 3" whats the problem plz help me. and my code is: ??? $dom = new DomDocument('1.0'); $dom->formatOutput = true; $dir = $dom->appendChild($dom->createElement('dir')); $db = new DB(); $db->connectDB(); $sqlSel = "SELECT image_name FROM tbl_uploaded WHERE customer_id='".$_SESSION['user_id']."' OR session_id='".$_SESSION['sessId']."'"; $rsSel = $db->openRs($sqlSel); $mydir = "uploaded/"; $d = dir($mydir); $i=1; while ($rowSel = $db->retRow($rsSel)){ $file = $dir->appendChild($dom->createElement('file', 'uploaded/'.$rowSel[0])); } $d->close(); $dom->formatOutput = true; $dom->save('dirbuilder.xml'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/54080-solved-need-help-understanding-oop-in-this-simple-suppose-to-be-script-using-dom/#findComment-364499 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.