Solved

Using xpath to produce and display parent and its child?

Posted on 2013-11-03
1
248 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

ASP.NET AJAX callbacks to web service are becoming very popular and unavoidable these days. The feel that the user gets when only a part of the page is updated without even hindering the UI is the advantage that the callbacks offer. The wait for the…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

680 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