本文整理汇总了Java中org.dom4j.XPath.setNamespaceContext方法的典型用法代码示例。如果您正苦于以下问题:Java XPath.setNamespaceContext方法的具体用法?Java XPath.setNamespaceContext怎么用?Java XPath.setNamespaceContext使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.dom4j.XPath
的用法示例。
在下文中一共展示了XPath.setNamespaceContext方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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;
}
示例2: 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());
}
示例3: 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());
}
示例4: InfoXML
import org.dom4j.XPath; //导入方法依赖的package包/类
/**
* Constructs the infoXML abstraction based on the input
* given.
*
* @param _xml
*/
public InfoXML ( String _xml )
{
try {
// TODO : BARFS on the xsi: attribute on return saying :
// org.dom4j.DocumentException: Error on line 1 of document :
// The prefix "xsi" for attribute "xsi:schemaLocation" associated with
// an element type "NSDLDataRepository" is not bound.
// Nested exception: The prefix "xsi" for attribute "xsi:schemaLocation"
// associated with an element type "NSDLDataRepository" is not bound.
this.contentRaw = _xml;
this.infoxmlDoc = DocumentHelper.parseText( this.contentRaw );
XPath xpath = DocumentHelper.createXPath( "//ndr:error" ); // for error node checks
// TODO : though the default namespace is NOT called NDR,
// http://www.xslt.com/html/xsl-list/2005-03/msg01059.html indicates we must give
// it one if we are to use it in an XPath. How fun is that!
SimpleNamespaceContext ns = new SimpleNamespaceContext();
ns.addNamespace("ndr", "http://ns.nsdl.org/ndr/response_v1.00/");
xpath.setNamespaceContext(ns);
// select the error node
Element error = (Element)xpath.selectSingleNode(this.infoxmlDoc);
if ( error != null )
{
this.hasErrors = true;
this.errorString = error.getText();
}
} catch ( Exception e ) {
this.contentRaw = _xml;
this.errorString = "Fatal error in InfoXML construction.";
// e.printStackTrace();
}
}
示例5: parse
import org.dom4j.XPath; //导入方法依赖的package包/类
/**
* 解析测试套件配置文件
* @param suiteInputStream 配置文件输入流
* @return 测试套件对象
* @throws DocumentException
*/
public Suite parse(InputStream suiteInputStream) throws DocumentException
{
SAXReader reader = new SAXReader();
reader.setEncoding("utf-8");
Document document = reader.read(suiteInputStream);
simpleNamespaceContext.addNamespace("ns", NS_URI);
XPath xpath = new DefaultXPath("/ns:suite");
xpath.setNamespaceContext(simpleNamespaceContext);
Element suiteEle = (Element) xpath.selectSingleNode(document);
if (suiteEle == null)
{
suiteEle = document.getRootElement();
// throw new RuntimeException("Can not found suite config.");
}
Suite suite = new Suite();
String xmlConfPath = suiteEle.attributeValue("pageConfig");
String pagePackage = suiteEle.attributeValue("pagePackage", "");
String rows = suiteEle.attributeValue("rows", "1");
String lackLines = suiteEle.attributeValue("lackLines", "nearby");
String errorLines = suiteEle.attributeValue("errorLines", "stop");
String afterSleep = suiteEle.attributeValue("afterSleep", "0");
suite.setXmlConfPath(xmlConfPath);
suite.setPagePackage(pagePackage);
suite.setRows(rows);
suite.setLackLines(lackLines);
suite.setErrorLines(errorLines);
suite.setAfterSleep(Long.parseLong(afterSleep));
pagesParse(document, suite);
return suite;
}
示例6: buildXpath
import org.dom4j.XPath; //导入方法依赖的package包/类
private static XPath buildXpath(Element xslt, final String functionCall) {
XPath xpath = xslt.createXPath(String.format(
"//xsl:value-of[@select='%s']", functionCall));
SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext();
namespaceContext.addNamespace("xsl",
"http://www.w3.org/1999/XSL/Transform");
xpath.setNamespaceContext(namespaceContext);
return xpath;
}
示例7: slurpOutput
import org.dom4j.XPath; //导入方法依赖的package包/类
public boolean slurpOutput(PipedOutputStream stdout, PipedOutputStream stderr) throws IOException {
log.log(Level.FINE, "--> SlurpOutput");
ImmutableMap<String, PipedOutputStream> streams = ImmutableMap.of("stdout", stdout, "stderr", stderr);
Document request = factory.newGetOutputRequest(shellId, commandId).build();
Document response = sendRequest(request);
XPath xpath = DocumentHelper.createXPath("//" + Namespaces.NS_WIN_SHELL.getPrefix() + ":Stream");
namespaceContext = new SimpleNamespaceContext();
namespaceContext.addNamespace(Namespaces.NS_WIN_SHELL.getPrefix(), Namespaces.NS_WIN_SHELL.getURI());
xpath.setNamespaceContext(namespaceContext);
Base64 base64 = new Base64();
for (Element e : (List<Element>) xpath.selectNodes(response)) {
PipedOutputStream stream = streams.get(e.attribute("Name").getText().toLowerCase());
final byte[] decode = base64.decode(e.getText());
log.log(Level.FINE, "piping " + decode.length + " bytes from " + e.attribute("Name").getText().toLowerCase());
stream.write(decode);
}
XPath done = DocumentHelper
.createXPath("//*[@State='http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done']");
done.setNamespaceContext(namespaceContext);
if (Iterables.isEmpty(done.selectNodes(response))) {
log.log(Level.FINE, "keep going baby!");
return true;
} else {
exitCode = Integer.parseInt(first(response, "//" + Namespaces.NS_WIN_SHELL.getPrefix() + ":ExitCode"));
log.log(Level.FINE, "no more output - command is now done - exit code: " + exitCode);
}
return false;
}