Solved

Using xpath to produce and display parent and its child?

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 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