SageTV Community  

Go Back   SageTV Community > SageTV Development and Customizations > SageTV Customizations
Forum Rules FAQs Community Downloads Today's Posts Search

Notices

SageTV Customizations This forums is for discussing and sharing user-created modifications for the SageTV application created by using the SageTV Studio or through the use of external plugins. Use this forum to discuss customizations for SageTV version 6 and earlier, or for the SageTV3 UI.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 04-15-2006, 05:36 PM
stanger89's Avatar
stanger89 stanger89 is offline
SageTVaholic
 
Join Date: May 2003
Location: Marion, IA
Posts: 15,188
Database help.

I'm sure there are some database gurus out there so I pose this question to you.

I want to be able to search an XML (DVD Profiler) on any field and get back a list of entries that contain that field.

Say there's this format:
Code:
<DVD>
  <Title>The Matrix</Title>
  <Actors>
    <Actor>
      <FirstName>Laurence</FirstName>
      <LastName>Fishburn</LastName>
      <Role>Morpheus</Role>
    </Actor>
  </Actors>
</DVD>
I want to be able to query for Actor == Laurence Fishburn and get back a list of DVDs (titles) containing Actor.

I'm looking for the easiest way, I know I don't need all that something like MySQL can deliver, but I also don't want to spend all my time coding something that's done already.
Reply With Quote
  #2  
Old 04-15-2006, 05:54 PM
GKusnick's Avatar
GKusnick GKusnick is offline
SageTVaholic
 
Join Date: Dec 2005
Posts: 5,083
In XPath this would be something like "DVD[Actors/Actor/(FirstName = 'Laurence' and LastName = 'Fishburn')]". So in standard DOM syntax you'd say something like:

Code:
nodelist = doc.selectNodes("DVD[Actors/Actor/(FirstName = 'Laurence' 
  and LastName = 'Fishburn')]");
This would return a list of all DVD nodes such that DVD/Actors/Actor/FirstName = 'Laurence' and DVD/Actors/Actor/LastName = 'Fishburn'. Then you just iterate through the list.

However I'm not real sure off the top of my head how you do DOM calls in Java. Maybe someone else can help with that.
__________________
-- Greg
Reply With Quote
  #3  
Old 04-15-2006, 07:00 PM
dflachbart dflachbart is offline
SageTVaholic
 
Join Date: Jan 2006
Location: Brookfield, CT
Posts: 2,743
Quote:
Originally Posted by GKusnick
In XPath this would be something like "DVD[Actors/Actor/(FirstName = 'Laurence' and LastName = 'Fishburn')]". So in standard DOM syntax you'd say something like:

Code:
nodelist = doc.selectNodes("DVD[Actors/Actor/(FirstName = 'Laurence' 
  and LastName = 'Fishburn')]");
This would return a list of all DVD nodes such that DVD/Actors/Actor/FirstName = 'Laurence' and DVD/Actors/Actor/LastName = 'Fishburn'. Then you just iterate through the list.

However I'm not real sure off the top of my head how you do DOM calls in Java. Maybe someone else can help with that.
I'm not familiar with the full XPath syntax, but if you want to return the titles shouldn't the XPath be "/DVD[Actors/Actor/(FirstName = 'Laurence' and LastName = 'Fishburn')]/Title" ?

Edit: I just realized that Gregs Xpath is also correct, you'll get back a list of DVD nodes and just need to pull out the title info manually...


To use XPath lookups in Java, you can use the Xalan package from Apache, it offers a full XPath implementation. There might also be something more lightweight out there which I don't know about. If you need help with using xalan, I could look this up, we are using it in a project at work ...


Dirk

Last edited by dflachbart; 04-15-2006 at 07:04 PM.
Reply With Quote
  #4  
Old 04-15-2006, 09:25 PM
stanger89's Avatar
stanger89 stanger89 is offline
SageTVaholic
 
Join Date: May 2003
Location: Marion, IA
Posts: 15,188
Looks like I have to break down and figure out XPath

Thanks for the suggestions guys.
Reply With Quote
  #5  
Old 04-15-2006, 10:08 PM
stanger89's Avatar
stanger89 stanger89 is offline
SageTVaholic
 
Join Date: May 2003
Location: Marion, IA
Posts: 15,188
Well, FYI, Java 5 (1.5.x) includes java.xml.xpath, which I'd kind of like to stick with since it minimizes the amount of stuff that must be installed.

So anyway, be gentile No laughing, at least not too much.

Trying to start easy, I've got a function, getTitles, which will return a list of the Titles stored in the XML. Originally it looked like this:

Code:
        for(int i = 0; i < numTitles; i++)
		{
			titles.add(getTitle(i));
			safeTitles.add(getSafeTitle(i));
		}

        public String getTitle(int n){
		Node node = DVDs.item(n);
		node.normalize();
		Node Property = node.getFirstChild();
		String value = "";
		
		while(Property.getNodeName() != "Title" && Property.getNextSibling() != null) {
			Property = Property.getNextSibling();
		}
		
		value = Property.getFirstChild().getNodeValue();
		return value;
	}

        public Vector getTitles()
	{
		return titles;
	}
How would I convert that to use XPath?
Reply With Quote
  #6  
Old 04-16-2006, 03:02 AM
GKusnick's Avatar
GKusnick GKusnick is offline
SageTVaholic
 
Join Date: Dec 2005
Posts: 5,083
I would do it roughly along the following lines:

Code:
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
NodeList nodes = xpath.evaluate(
  "//DVD[Actors/Actor/(FirstName = 'Laurence' and LastName = 'Fishburn')]/Title", 
  DVDs, NODESET);

for (int i = 0; i < nodes.getLength(); i++)
    {
    titles.add(nodes.item(i).getTextContent());
    safeTitles.add(getSafeTitle(i));
    }
Please note that I'm looking at java.xml.xpath for the first time here, and have not made any attempt to compile or test this code. But from my reading of the javadocs, this is more or less how it should go.

That's a fairly complex XPath expression that may not be exactly right, so my advice would be to start with a much simpler expression such as "//Title" (which simply retrieves all the Title nodes anywhere in the XML tree), get your code working with that, and then build up the more complex expression step by step. So for instance, once you get "//Title" working, try "//DVD/Title" (i.e. all Title nodes that are direct children of DVD nodes), then "//DVD[Actors]/Title" (all Title nodes that are direct children of DVD nodes that have Actors), and so on. That way when you screw it up, you'll know exactly where the screwup happened.
__________________
-- Greg
Reply With Quote
  #7  
Old 04-16-2006, 08:36 AM
stanger89's Avatar
stanger89 stanger89 is offline
SageTVaholic
 
Join Date: May 2003
Location: Marion, IA
Posts: 15,188
Cool, thanks. I've got the getTitles() working with XPath
The code for anyone interested:
Code:
    public Vector getTitles()
    {    
        NodeList nodes = null;
        Vector result = new Vector();
        try {
            nodes =  (NodeList) xpath.evaluate("//DVD/Title", document, XPathConstants.NODESET);
        } catch (XPathExpressionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for (int i = 0; i < nodes.getLength(); i++)
        {
            result.add(nodes.item(i).getTextContent());
        }
        return result;
    }
Time to work on figuring out XPath and what all it can do
Reply With Quote
  #8  
Old 04-16-2006, 03:58 PM
stanger89's Avatar
stanger89 stanger89 is offline
SageTVaholic
 
Join Date: May 2003
Location: Marion, IA
Posts: 15,188
FYI,

I found a really cool interactive XPath evaluator:
http://www.develop.com/technology/re...0-795f9aebcebf

You can load your own XMLs into it and all
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 07:34 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2023, vBulletin Solutions Inc.
Copyright 2003-2005 SageTV, LLC. All rights reserved.