Women in Technology

Hear us Roar



Article:
  Parsing an XML Document with XPath
Subject:   Parsing a Namespace node with JDK 5.0
Date:   2005-01-14 08:23:02
From:   dvohra09
To parse a namespace node with the JDK 5.0 javax.xml.xpath package, set the NamespaceContext on the XPath object.



XPath xpath;
NamespaceContext namepsaceContext=new NamespaceContextImpl();
xpath.setNamespaceContext(namespaceContext);


For a NamespaceContext object, create a class which implements the NamespaceContext interface.



private class NamespaceContextImpl implements NamespaceContext{


public String uri;
public String prefix;


public String getUri(){
return uri;
}
public void setUri(String uri){
this.uri=uri;
}


public String getPrefix(){
return prefix;
}


public void setPrefix(String prefix){
this.prefix=prefix;
}



}


Main Topics Oldest First

Showing messages 1 through 2 of 2.

  • Parsing a Namespace node with JDK 5.0
    2005-02-17 07:55:52  Gerg [View]

    Thanks for the examples. Could you please also show an example of an xpath that includes two different namespaces prefixes? If a journal's article could be in different languages, you might want to add an "xml:lang" attribute to the "article" element. (Where the "xml" prefix in XML documents implicitly refers to the "http://www.w3.org/XML/1998/namespace" namespace without being explicitly declared as such with an "xmlns" attribute in the root element - as I understand it.) Presumably, the xpath expression to return the value of that attribute would be "/catalog/journal:journal/article/@xml:lang". In such an expression it would seem that we need to xpath.setNamespaceContext(NamespaceContextImpl) twice, with two different NamespaceContextImpl instances, one for each prefix ("journal" and "xml"). For me, though, evaluating this expression returns nothing, even if I use a NamespaceContextImpl class (or two). Hmmmmmm.
  • Parsing a Namespace node with JDK 5.0
    2005-01-14 08:38:33  Deepak Vohra | [View]

    Include the interface methods in the implementation class.

    An implementation of NamespaceContext with a single prefix corresponding to a namespace uri.

    public class NamespaceContextImpl implements NamespaceContext{

    public String uri;
    public String prefix;

    public NamespaceContextImpl(){}

    public NamespaceContextImpl(String prefix, String uri){
    this.uri=uri;
    this.prefix=prefix;
    }

    public String getNamespaceURI(String prefix){
    return uri;
    }
    public void setNamespaceURI(String uri){
    this.uri=uri;
    }

    public String getPrefix(String uri){
    return prefix;
    }

    public void setPrefix(String prefix){
    this.prefix=prefix;
    }
    public Iterator getPrefixes(String uri){return null;}

    }