Solved

Using xpath to produce and display parent and its child?

Posted on 2013-11-03
1
246 Views
Last Modified: 2013-11-08
HTML
 
      <div id="nav"></div>
       <ul>
         <li><span id="parent" >main</span></li>
         <li><span id="chapter">chapter</span></li>
         <li><span id="subchapter">subchapter</span></li>
       </ul>
       <div id="result"> 
        
      </div>

Open in new window


XML:

   
   
<section id="parent">
    	<data>main</data>
    	<chapter id="chapter">
    		<data>Chapter 1</data>
    		<subchapter id="subchapter">
    			<data>Subchapter 1.1</data>
    			<topic id="634322">
    				<data>Topic A</data>
    				<paragraph id="2373" level="1">example1</paragraph>
    				<paragraph id="3438" level="2">example2.</paragraph>
    				<paragraph id="4549" level="1">exampl3</paragraph>
    			</topic>
                   </subchapter>
               ....
    	</chapter>
         ...
    </section>
    ....

Open in new window



Using Xpath and javascript or jquery to produce (id of nav map to id of xml):

on click `id=parent` or `chapter` or `subchapter` get and display:
       main
         chapter
           Subchapter 1.1
             Topic A
               example1
                 example2
               exampl3

Open in new window

      main
         chapter
           Subchapter 1.1
             Topic A
               example1
                 example2
               exampl3


How can I get this the above result using xpath then display result on `<div id="result">`

thanks
0
Comment
Question by:pelnisgproup
1 Comment
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 500 total points
ID: 39621202
So you're making an ajax call on each node expansion to get it's children is that?
In response you get XML and want to parse it so that you'll be able to inject the new nodes under the parent... correct?

My first question is: Can't you return json instead of XML?
Dealing with XML in javascript is slow and a mess...
http://www.w3schools.com/xml/xml_parser.asp

Basically you can parse your response xml string like:
var txt = '-> XML String goes here <-';
var xmlDoc;
if (window.DOMParser)
  {
  var parser=new DOMParser();
  xmlDoc=parser.parseFromString(txt,"text/xml");
  }
else // Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async=false;
  xmlDoc.loadXML(txt);
  } 

Open in new window

after this, xmlDoc will contain the structure of your XML data and you can access it normally with the dot notation.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP and Soap 3 52
MS SQL Database Generating XML using query 21 44
XML XSLT If loop 4 26
file upload jquery, send variables 3 25
There is basically two types of AJAX request in mootools. Request  and Request.HTML Request: Request is the basic XHR request class in MooTools. While not extremely useful on its own, it provides the basic functionality for both Request.HTM…
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question