?
Solved

Using xpath to produce and display parent and its child?

Posted on 2013-11-03
1
Medium Priority
?
251 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 1500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Today I am discussing AJAX problems in IE7 and I bet this will helps many guys out here who have problems with AJAX work. Lets start with the discovery of problem and then we will talk about its different solutions. My last two projects included …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

770 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