Thursday, 28 July 2011

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[] argsthrows Exception {
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(new File("tds.xml"));
        Attribute attr = (AttributeXPath.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));

    }

}

   
    
  

No comments:

Post a Comment