|
Academic Work Personal
|
Java /
XPath with JDOM
This code snippet is copied from http://www.java2s.com/Code/Java/XML/XPathwithJDOM.htm .
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;
public class MainClass {
public static void main(String[] args) throws Exception {
Namespace NS_GUEST = Namespace.getNamespace("g", "uri:c:guest");
Namespace NS_SCHEDULE = Namespace.getNamespace("s", "uri:c:schedule");
SAXBuilder builder = new SAXBuilder();
Document document = builder.build(new File("tds_ns.xml"));
XPath xPath = XPath.newInstance("/s:schedule/s:show[@date=$date]/g:guest"); // HERE
xPath.addNamespace(NS_SCHEDULE);
xPath.addNamespace(NS_GUEST);
String formattedDate = new SimpleDateFormat("MM.dd.yy").format(new Date(2006, 5, 14));
xPath.setVariable("date", formattedDate);
Element guest = (Element) xPath.selectSingleNode(document); // HERE
System.out.println(guest.getChildText("name", NS_GUEST));
}
}
|
![]() anime | bash | blogs | bsd | c/c++ | c64 | calc | comics | convert | cube | del.icio.us | digg | east | eBooks | egeszseg | elite | firefox | flash | fun | games | gimp | google | groovy | hardware | hit&run | howto | java | javascript | knife | lang | latex | liferay | linux | lovecraft | magyar | maths | movies | music | p2p | perl | pdf | photoshop | php | pmwiki | prog | python | radio | recept | rts | scala | scene | sci-fi | scripting | security | shell | space | súlyos | telephone | torrente | translate | ubuntu | vim | wallpapers | webutils | wikis | windows Blogs and Dev. * Ubuntu Incident Places Debrecen | France | Hungary | Montreal | Nancy Notes Hobby Projects * Jabba's Codes Quick Links [ edit ] |