0

Somehow I keep getting error in javascript when I try to parse xml from php string, my code is like:

<?php
$xml = simplexml_load_file('file.xml');
$products = $xml->xpath("/products/product[@model='".$model . "']");
$filtered_xml = $products[0]->asXML();
?>

<script> 
alert( $.parseXML( '<?php echo $filtered_xml;?>'  ).find('name').text() );
</script>

echo $filtered_xml is returning a well formed xml as I am looking for, but something in the javascript - $.parseXML( '<?php echo $filtered_xml;?>' ) is causing errors. Thanks in advance for any help.

3
  • 2
    Could you specify the error you are getting? Commented Mar 14, 2012 at 5:34
  • 3
    can you show the markup rendered on the browser? Commented Mar 14, 2012 at 5:42
  • it's not executing the php i bet. use double quotes? Commented Mar 14, 2012 at 9:36

1 Answer 1

1

$.parseXML() itself does not return a jQuery object. Look at example in docs

http://api.jquery.com/jQuery.parseXML/

Proper use in your case would look more like:

  var xml= $.parseXML( '<?php echo $filtered_xml;?>') ;

   alert( $(xml).find('name').text() )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.