XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 13th, 2004, 03:39 PM
XPathDummy XPathDummy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 4 XPathDummy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Red face Quick XPath/Dom4J question.

I am able to retrieve date from an XML file using XPath and turn them into a org.dom4j.Element. However, when I print the full path of the Element by using Element.getPath(), I don't get all the info I need.

What I need is this:
Path = /aaa/bbb=567/ccc=123/ddd

But what I get from getPath() is this:
Path = /aaa/bbb/ccc/ddd

Is there any way to get the attributes in the path?

Thanks.

Reply With Quote
  #2  
Old October 15th, 2004, 03:45 PM
XPathDummy XPathDummy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 4 XPathDummy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Anyone?

Reply With Quote
  #3  
Old October 17th, 2004, 02:00 PM
Wingman Wingman is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Bavaria, Germany
Posts: 140 Wingman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 40 m 41 sec
Reputation Power: 6
I don't think there's something built-in as you want it, but it's not that hard (if I understand you right):

Code:
string GetFullyQualifiedXPath(Element ele) {
   string fullPath = "";

   Element current = ele;
   do {
       strung currentPath += "*[local-name() = '" + current.LocalName + "' && namespace-uri() = '" + current.NamespaceUri + "'";                
       foreach (Attribute attr in ele.Attributes) {
          currentPath += " && @" + attr.Name + " = '" + attr.Value + "'";
       }
       currentPath += "]";
       fullPath = currentPath + "/" + fullPath;	   
       current = current.ParentNode;
   } while (current != null);
   
   return fullPath;
}


Above code won't compile in java, it's just some pseudo code. It will generate a full qualified x-path to any element in your document.

Example:

Code:
<Document>
	<Root>
		<Child Key1="Value" />
	</Root>
</Document>


For example, a call of GetFullyQualifiedXPath for Node "Child" will return:
/*[local-name() = 'Document' && namespace-uri() = '']/*[local-name() = 'Root' && namespace-uri() = '']/*[local-name() = 'Child' && namespace-uri() = '' && @Key1 = 'Value']

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > Quick XPath/Dom4J question.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT