1

my code for html is this

 <script type="text/javascript" src="js/jquery.js"></script>
 <script type="text/javascript">
 if (window.XMLHttpRequest)
 {
     xmlhttp=new XMLHttpRequest();
 } else {
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }

 xmlhttp.open("GET","brands.xml",false);
 xmlhttp.send();
 theXmlDoc=xmlhttp.responseXML; 
 function fillForm(){
     $(theXmlDoc).find('table[name=brands]').each(function(){
         alert($(this));//doesn't fire when brands.xml contains more than one entry of <table name="brands"> else shows Object object
     });

My brands.xml is

  <table name="brands">
        <column name="BrandID">1</column>
        <column name="BrandName">AX</column>
        <column name="CompanyInfo">FDC</column>
        <column name="Composition">Cap</column>
 </table>
 <table name="brands">
        <column name="BrandID">2</column>
        <column name="BrandName">UP</column>
        <column name="CompanyInfo">Tor</column>
        <column name="Composition">Asp</column>
 </table>

when brands.xml contains single entry of <table name="brands"> alert shows Object object but when i include more than one table name as shown above each is not getting executed.

2
  • Is there a specific reason you're using jQuery to traverse the XML, but not retrieve it? Commented Apr 12, 2012 at 12:26
  • i think jquery has lots of methods to handle stuff,if there is better alternative to access xml from html pls tell me ,and i want to retrieve text but i can only do that once i can loop from xml nodes Commented Apr 12, 2012 at 12:39

2 Answers 2

2

Your XML will need to be wrapped by a single node:

<tables>
    <table name="brands">
        <column name="BrandID">1</column>
        <column name="BrandName">AX</column>
        <column name="CompanyInfo">FDC</column>
        <column name="Composition">Cap</column>
    </table>
    <table name="brands">
        <column name="BrandID">2</column>
        <column name="BrandName">UP</column>
        <column name="CompanyInfo">Tor</column>
        <column name="Composition">Asp</column>
    </table>
</tables>

And you'll need to adjust your JavaScript accordingly so select inside this wrapping node.

Sign up to request clarification or add additional context in comments.

Comments

1

you need to specify a single root node above the table nodes.

like

<root-node>
 <table name="brands">
        <column name="BrandID">1</column>
        <column name="BrandName">AX</column>
        <column name="CompanyInfo">FDC</column>
        <column name="Composition">Cap</column>
    </table>
    <table name="brands">
        <column name="BrandID">2</column>
        <column name="BrandName">UP</column>
        <column name="CompanyInfo">Tor</column>
        <column name="Composition">Asp</column>
    </table>
</root-node>

see the tutorial http://webhole.net/2009/12/16/how-to-read-xml-with-javascript/

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.