Get and set value through XPath : XPath « XML « Java Tutorial






import java.io.FileReader;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Attr;
import org.xml.sax.InputSource;

public class GetNameAsAttr {

  public static void main(String[] args) throws Exception {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();

    Attr result = (Attr) xPath.evaluate("/schedule/@name", new InputSource(
        new FileReader("tds.xml")), XPathConstants.NODE);
    System.out.println(result.getValue());

    result.setValue("The Colbert Report");

  }
}








33.16.XPath
33.16.1.Search an XPath
33.16.2.Get and set value through XPath
33.16.3.XPath.evaluate("/schedule/@seriesId")
33.16.4.Return a NodeList
33.16.5.Get integer value
33.16.6.Get the W3C NodeList instance associated with the XPath selection supplied
33.16.7.Get the String data associated with the XPath selection supplied