Use XPath to select node : XPath « XML « Java






Use XPath to select node

    


import java.io.File;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;

public class MainClass {
    public static void main(String[] args) throws Exception {
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(new File("tds.xml"));
        Attribute attr = (Attribute) XPath.selectSingleNode(doc, "/schedule/@name");
        System.out.println(attr.getValue());
        
        Element el = new Element("parent").addContent(new Element("child").setAttribute("name", "value"));
        System.out.println(XPath.selectSingleNode(new Attribute("foo", "bar"), "position()"));
        
        XPath path = XPath.newInstance("/schedule/@name");
        System.out.println(path.valueOf(doc));

    }

}

   
    
    
  








Related examples in the same category

1.Create an XML document and search by XPath
2.Shallow print of node list
3.Deep print of node list
4.Parse with XPath
5.XML and XPath utilities
6.Get the String data associated with the XPath selection supplied
7.This program evaluates XPath expressions