本文整理汇总了Java中org.dom4j.XPath类的典型用法代码示例。如果您正苦于以下问题:Java XPath类的具体用法?Java XPath怎么用?Java XPath使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
XPath类属于org.dom4j包,在下文中一共展示了XPath类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getRecordCount
import org.dom4j.XPath; //导入依赖的package包/类
public int getRecordCount ()
{
Map<String,String> uris = new HashMap<String,String>();
uris.put( "ddsws", "http://www.dlese.org/Metadata/ddsws" );
uris.put( "groups", "http://www.dlese.org/Metadata/groups/" );
uris.put( "adn", "http://adn.dlese.org" );
uris.put( "annotation", "http://www.dlese.org/Metadata/annotation" );
XPath xpath = DocumentHelper.createXPath( "//ddsws:results/ddsws:record" );
xpath.setNamespaceURIs( uris );
List recordNodes = xpath.selectNodes(this.document);
return recordNodes.size();
}
示例2: getTagElement
import org.dom4j.XPath; //导入依赖的package包/类
private Element getTagElement (String _tag, String _namespacePrefix, String _namespaceURI ) {
Element tagElement = null; // caller is responsible for handling null
try {
// TODO this isn't complete, as we can have any arbitrary tag and values for them infinitely deep
XPath xpath = DocumentHelper.createXPath( "//" + ( _namespacePrefix != null ? _namespacePrefix + ":" : "" ) + _tag.trim() );
SimpleNamespaceContext ns = new SimpleNamespaceContext();
ns.addNamespace( _namespacePrefix, _namespaceURI );
xpath.setNamespaceContext(ns);
tagElement = ((Element)xpath.selectSingleNode(this.infoxmlDoc));
} catch ( Exception e ) {
e.printStackTrace();
}
return tagElement;
}
示例3: shouldBuildDocumentFromSetOfXPaths
import org.dom4j.XPath; //导入依赖的package包/类
@Test
public void shouldBuildDocumentFromSetOfXPaths() throws XPathExpressionException, IOException {
Map<String, Object> xmlProperties = fixtureAccessor.getXmlProperties();
Document builtDocument = new XmlBuilder(namespaceContext)
.putAll(xmlProperties.keySet())
.build(DocumentHelper.createDocument());
for (Entry<String, Object> xpathToValuePair : xmlProperties.entrySet()) {
XPath xpath = builtDocument.createXPath(xpathToValuePair.getKey());
if (null != namespaceContext) {
xpath.setNamespaceContext(new SimpleNamespaceContextWrapper(namespaceContext));
}
assertThat(xpath.evaluate(builtDocument)).isNotNull();
}
assertThat(xmlToString(builtDocument)).isEqualTo(fixtureAccessor.getPutXml());
}
示例4: shouldBuildDocumentFromSetOfXPathsAndSetValues
import org.dom4j.XPath; //导入依赖的package包/类
@Test
public void shouldBuildDocumentFromSetOfXPathsAndSetValues() throws XPathExpressionException, IOException {
Map<String, Object> xmlProperties = fixtureAccessor.getXmlProperties();
Document builtDocument = new XmlBuilder(namespaceContext)
.putAll(xmlProperties)
.build(DocumentHelper.createDocument());
for (Entry<String, Object> xpathToValuePair : xmlProperties.entrySet()) {
XPath xpath = builtDocument.createXPath(xpathToValuePair.getKey());
if (null != namespaceContext) {
xpath.setNamespaceContext(new SimpleNamespaceContextWrapper(namespaceContext));
}
assertThat(xpath.selectSingleNode(builtDocument).getText()).isEqualTo(xpathToValuePair.getValue());
}
assertThat(xmlToString(builtDocument)).isEqualTo(fixtureAccessor.getPutValueXml());
}
示例5: getElement
import org.dom4j.XPath; //导入依赖的package包/类
public Element getElement( String xpathExpr )
throws XMLException
{
XPath xpath = createXPath( xpathExpr );
Object evaluated = xpath.selectSingleNode( document );
if ( evaluated == null )
{
return null;
}
if ( evaluated instanceof Element )
{
return (Element) evaluated;
}
else
{
// Unknown evaluated type.
throw new XMLException( ".getElement( Expr: " + xpathExpr + " ) resulted in non-Element type -> ("
+ evaluated.getClass().getName() + ") " + evaluated );
}
}
示例6: getElementText
import org.dom4j.XPath; //导入依赖的package包/类
public String getElementText( Node context, String xpathExpr )
throws XMLException
{
XPath xpath = createXPath( xpathExpr );
Object evaluated = xpath.selectSingleNode( context );
if ( evaluated == null )
{
return null;
}
if ( evaluated instanceof Element )
{
Element evalElem = (Element) evaluated;
return evalElem.getTextTrim();
}
else
{
// Unknown evaluated type.
throw new XMLException( ".getElementText( Node, Expr: " + xpathExpr + " ) resulted in non-Element type -> ("
+ evaluated.getClass().getName() + ") " + evaluated );
}
}
示例7: getXmlUser
import org.dom4j.XPath; //导入依赖的package包/类
public void getXmlUser(Document doc, Map<String, String> userColumn) {
// 根据xpath方式得到所要得到xml文档的具体对象,根据分析解析xml文档可知,xml文档中含有前缀名
Map<String, String> map = new HashMap<String, String>();
map.put("c", "collection");
// 根据xml文档,//c:Table 即为得到的文档对象
XPath path = doc.createXPath("//c:Users");
path.setNamespaceURIs(map);
List<Element> list = path.selectNodes(doc);
// 得到tables对象,该对象是该pdm文件中所有表的集合
for (Element element : list) {
for (Iterator<Element> iter = element.elementIterator("User"); iter
.hasNext();) {
Element userElement = iter.next();
Attribute attribute = userElement.attribute("Id");
userColumn.put(attribute.getText(),
userElement.elementText("Code"));
// setUserlist(userElement.elementText("Code"));
}
}
}
示例8: isMysql
import org.dom4j.XPath; //导入依赖的package包/类
public boolean isMysql(Document doc) {
// 根据xpath方式得到所要得到xml文档的具体对象,根据分析解析xml文档可知,xml文档中含有前缀名
Map<String, String> map = new HashMap<String, String>();
map.put("c", "collection");
// 根据xml文档,//c:Table 即为得到的文档对象
XPath path = doc.createXPath("//c:DBMS");
path.setNamespaceURIs(map);
List<Element> list = path.selectNodes(doc);
// 得到tables对象,该对象是该pdm文件中所有表的集合
for (Element element : list) {
for (Iterator<Element> iter = element.elementIterator("Shortcut"); iter
.hasNext();) {
Element userElement = iter.next();
String s = userElement.elementText("Code").toUpperCase();
if (s.substring(0, 5).equals("MYSQL")) {
logger.debug(s);
return true;
} else if (s.substring(0, 6).equals("ORACLE")) {
logger.debug(s);
return false;
}
}
}
return false;
}
示例9: getAttributeValue
import org.dom4j.XPath; //导入依赖的package包/类
/**
* Extracts the value of the supplied attribute
*
* @param element
* element with attribute in question
* @param attributeName
* name of the attribute in question
* @param failIfNotFound
* determines if exception should be thrown if attribute is not
* found
* @return value of the attribute in question, null if not found and
* failIfNotFound is set to false
* @throws GenericArtifactParsingException
* exception thrown is attribute is missing and failIfNotFound
* is set to true
*/
public static String getAttributeValue(Element element,
String attributeName, boolean failIfNotFound)
throws GenericArtifactParsingException {
XPath xpath = new DefaultXPath("@" + attributeName);
xpath.setNamespaceURIs(ccfNamespaceMap);
Node attributeNode = xpath.selectSingleNode(element);
if (attributeNode == null) {
if (failIfNotFound) {
throw new GenericArtifactParsingException("Missing attribute: "
+ attributeName + " in element " + element.getName());
} else {
return null;
}
} else {
return attributeNode.getText();
}
}
示例10: convertTableSettings
import org.dom4j.XPath; //导入依赖的package包/类
private void convertTableSettings(final Document doc) {
XPath xpathSelector = DocumentHelper.createXPath("/settings/columns/column");
List<?> results = xpathSelector.selectNodes(doc);
List<String> tableColumnNames = new ArrayList<String>();
List<String> tableColumnVisible = new ArrayList<String>();
for (Iterator<?> iter = results.iterator(); iter.hasNext();) {
Element element = (Element) iter.next();
Attribute name = element.attribute("name");
Attribute visible = element.attribute("visible");
tableColumnNames.add(name.getText());
if (visible.getText().equals("true")) {
tableColumnVisible.add(name.getText());
}
}
String mode = convertFlag(doc);
writeTableSettings(doc, mode, tableColumnNames, tableColumnVisible);
}
示例11: convertFlag
import org.dom4j.XPath; //导入依赖的package包/类
private String convertFlag(final Document doc) {
XPath flagSelector = DocumentHelper.createXPath("/settings/flags/flag");
List<?> flagResults = flagSelector.selectNodes(doc);
boolean text = false;
boolean window = false;
for (Iterator<?> iter = flagResults.iterator(); iter.hasNext();) {
Element element = (Element) iter.next();
Attribute key = element.attribute("key");
Attribute visible = element.attribute("enabled");
if (key.getText().equals("FLAG_AUTO_RESIZE_COLUMNS_TEXT")) {
text = visible.getText().equals("true");
element.detach();
}
if (key.getText().equals("FLAG_AUTO_RESIZE_COLUMNS_WINDOW")) {
window = visible.getText().equals("true");
element.detach();
}
}
if (text) {
return "TEXT";
}
if (window) {
return "WINDOW";
}
return "NONE";
}
示例12: getXPath
import org.dom4j.XPath; //导入依赖的package包/类
/** Get an XPath version of the given dotted path. A dotted path
* foo.bar.baz corresponds to the XML node <foo><bar><baz>
* </baz></bar></foo>
*
* Implementation note: If needed, this could be optimized by keeping a
* HashMap cache of the XPaths, since they don't change.
*
* @param path A dotted path
* @return An XPath that matches the dotted path equivalent, using
* "dk:" as namespace prefix for all but the first element.
*/
private XPath getXPath(String path) {
String[] pathParts = path.split("\\.");
StringBuilder result = new StringBuilder();
result.append("/");
result.append(pathParts[0]);
for (int i = 1; i < pathParts.length; i++) {
result.append("/dk:");
result.append(pathParts[i]);
}
XPath xpath = xmlDoc.createXPath(result.toString());
Namespace nameSpace = xmlDoc.getRootElement().getNamespace();
Map<String, String> namespaceURIs = new HashMap<String, String>(1);
namespaceURIs.put("dk", nameSpace.getURI());
xpath.setNamespaceURIs(namespaceURIs);
return xpath;
}
示例13: applyXPath
import org.dom4j.XPath; //导入依赖的package包/类
@SuppressWarnings("unchecked")
private boolean applyXPath()
{
try{
XPath xpath = data.document.createXPath(data.PathValue);
if(meta.isNamespaceAware())
{
xpath = data.document.createXPath(addNSPrefix(data.PathValue, data.PathValue));
xpath.setNamespaceURIs(data.NAMESPACE);
}
// get nodes list
data.an = (List<AbstractNode>) xpath.selectNodes(data.document);
data.nodesize=data.an.size();
data.nodenr=0;
}catch (Exception e)
{
log.logError(toString(),Messages.getString("GetXMLData.Log.ErrorApplyXPath",e.getMessage()));
return false;
}
return true;
}
示例14: applyXPath
import org.dom4j.XPath; //导入依赖的package包/类
@SuppressWarnings("unchecked")
private boolean applyXPath()
{
try{
XPath xpath = data.document.createXPath(data.PathValue);
if(meta.isNamespaceAware())
{
xpath = data.document.createXPath(addNSPrefix(data.PathValue, data.PathValue));
xpath.setNamespaceURIs(data.NAMESPACE);
}
// get nodes list
data.an = (List<AbstractNode>) xpath.selectNodes(data.document);
data.nodesize=data.an.size();
data.nodenr=0;
}catch (Exception e)
{
logError(BaseMessages.getString(PKG, "GetXMLData.Log.ErrorApplyXPath",e.getMessage()));
return false;
}
return true;
}
示例15: applyXPath
import org.dom4j.XPath; //导入依赖的package包/类
@SuppressWarnings( "unchecked" )
private boolean applyXPath() {
try {
XPath xpath = data.document.createXPath( data.PathValue );
if ( meta.isNamespaceAware() ) {
xpath = data.document.createXPath( addNSPrefix( data.PathValue, data.PathValue ) );
xpath.setNamespaceURIs( data.NAMESPACE );
}
// get nodes list
data.an = xpath.selectNodes( data.document );
data.nodesize = data.an.size();
data.nodenr = 0;
} catch ( Exception e ) {
logError( BaseMessages.getString( PKG, "GetXMLData.Log.ErrorApplyXPath", e.getMessage() ) );
return false;
}
return true;
}