org.dom4j.Node类的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(10.7k)|赞(0)|评价(0)|浏览(192)

本文整理了Java中org.dom4j.Node类的一些代码示例,展示了Node类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Node类的具体详情如下:
包路径:org.dom4j.Node
类名称:Node

Node介绍

[英]Node defines the polymorphic behavior for all XML nodes in a dom4j tree. A node can be output as its XML format, can be detached from its position in a document and can have XPath expressions evaluated on itself. A node may optionally support the parent relationship and may be read only.
[中]Node定义dom4j树中所有XML节点的多态行为。节点可以作为其XML格式输出,可以从文档中的位置分离,并且可以对自身计算XPath表达式。节点可以选择支持父关系,并且可以是只读的。

代码示例

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void withoutItemsEnumBindTarget() throws Exception {
  3. BeanWithEnum testBean = new BeanWithEnum();
  4. testBean.setTestEnum(TestEnum.VALUE_2);
  5. getPageContext().getRequest().setAttribute("testBean", testBean);
  6. this.tag.setPath("testEnum");
  7. int result = this.tag.doStartTag();
  8. assertEquals(Tag.SKIP_BODY, result);
  9. String output = "<div>" + getOutput() + "</div>";
  10. SAXReader reader = new SAXReader();
  11. Document document = reader.read(new StringReader(output));
  12. Element rootElement = document.getRootElement();
  13. assertEquals(2, rootElement.elements().size());
  14. Node value1 = rootElement.selectSingleNode("//input[@value = 'VALUE_1']");
  15. Node value2 = rootElement.selectSingleNode("//input[@value = 'VALUE_2']");
  16. assertEquals("TestEnum: VALUE_1",
  17. rootElement.selectSingleNode("//label[@for = '" + value1.valueOf("@id") + "']").getText());
  18. assertEquals("TestEnum: VALUE_2",
  19. rootElement.selectSingleNode("//label[@for = '" + value2.valueOf("@id") + "']").getText());
  20. assertEquals(value2, rootElement.selectSingleNode("//input[@checked]"));
  21. }

代码示例来源:origin: pentaho/pentaho-kettle

  1. private void addLoopXPath( Node node, IProgressMonitor monitor ) {
  2. Element ce = (Element) node;
  3. monitor.worked( 1 );
  4. // List child
  5. for ( int j = 0; j < ce.nodeCount(); j++ ) {
  6. if ( monitor.isCanceled() ) {
  7. return;
  8. }
  9. Node cnode = ce.node( j );
  10. if ( !Utils.isEmpty( cnode.getName() ) ) {
  11. Element cce = (Element) cnode;
  12. if ( !listpath.contains( cnode.getPath() ) ) {
  13. nr++;
  14. monitor.subTask( BaseMessages.getString( PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.FetchNodes",
  15. String.valueOf( nr ) ) );
  16. monitor.subTask( BaseMessages.getString( PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.AddingNode",
  17. cnode.getPath() ) );
  18. listpath.add( cnode.getPath() );
  19. }
  20. // let's get child nodes
  21. if ( cce.nodeCount() > 1 ) {
  22. addLoopXPath( cnode, monitor );
  23. }
  24. }
  25. }
  26. }

代码示例来源:origin: igniterealtime/Openfire

  1. protected void writeNode(Node node) throws IOException {
  2. int nodeType = node.getNodeType();
  3. switch (nodeType) {
  4. case Node.ELEMENT_NODE:
  5. writeCDATA(node.getText());
  6. break;
  7. case Node.ENTITY_REFERENCE_NODE:
  8. break;
  9. case Node.COMMENT_NODE:
  10. writeComment(node.getText());
  11. break;
  12. case Node.DOCUMENT_NODE:

代码示例来源:origin: igniterealtime/Openfire

  1. private Map<String, String> readInitParams(Node configData) {
  2. Map<String, String> paramMap = new HashMap<>();
  3. List<Node> params = configData.selectNodes("init-params/init-param");
  4. for (Node param : params) {
  5. String paramName = param.selectSingleNode("param-name").getStringValue();
  6. String paramValue = param.selectSingleNode("param-value").getStringValue();
  7. paramMap.put(paramName, paramValue);
  8. }
  9. return paramMap;
  10. }

代码示例来源:origin: jaxen/jaxen

  1. public Object getParentNode(Object contextNode)
  2. {
  3. if ( contextNode instanceof Node )
  4. {
  5. Node node = (Node) contextNode;
  6. Object answer = node.getParent();
  7. if ( answer == null )
  8. {
  9. answer = node.getDocument();
  10. if (answer == contextNode) {
  11. return null;
  12. }
  13. }
  14. return answer;
  15. }
  16. return null;
  17. }

代码示例来源:origin: spotbugs/spotbugs

  1. private static String getChildText(Node node, String childName) throws PluginException {
  2. Node child = node.selectSingleNode(childName);
  3. if (child == null) {
  4. throw new PluginException("Could not find child \"" + childName + "\" for node");
  5. }
  6. return child.getText();
  7. }

代码示例来源:origin: igniterealtime/Openfire

  1. final SAXReader saxReader = new SAXReader();
  2. saxReader.setEncoding( "UTF-8" );
  3. final Document pluginXML = saxReader.read( pluginConfig.toFile() );
  4. final String className = pluginXML.selectSingleNode( "/plugin/class" ).getText().trim();
  5. final Plugin plugin = (Plugin) pluginLoader.loadClass( className ).newInstance();
  6. final Element adminElement = (Element) pluginXML.selectSingleNode( "/plugin/adminconsole" );
  7. if ( adminElement != null )
  8. final Element appName = (Element) adminElement.selectSingleNode( "/plugin/adminconsole/global/appname" );
  9. if ( appName != null )
  10. appName.addAttribute( "plugin", canonicalName );
  11. Element imageEl = (Element) adminElement.selectSingleNode( "/plugin/adminconsole/global/logo-image" );
  12. if ( imageEl != null )

代码示例来源:origin: jenkinsci/jenkins

  1. Object result;
  2. try {
  3. Document dom = new SAXReader().read(new StringReader(sw.toString()));
  4. XPath xExclude = dom.createXPath(exclude);
  5. xExclude.setFunctionContext(functionContext);
  6. List<org.dom4j.Node> list = (List<org.dom4j.Node>)xExclude.selectNodes(dom);
  7. for (org.dom4j.Node n : list) {
  8. Element parent = n.getParent();
  9. if(parent!=null)
  10. parent.remove(n);
  11. result = dom;
  12. } else {
  13. XPath comp = dom.createXPath(xpath);
  14. comp.setFunctionContext(functionContext);
  15. List list = comp.selectNodes(dom);
  16. for (Object o : list) {
  17. if (o instanceof String) {
  18. root.addText(o.toString());
  19. } else {
  20. root.add(((org.dom4j.Node)o).detach());

代码示例来源:origin: stackoverflow.com

  1. SAXReader reader = new SAXReader();
  2. Document document = reader.read(file);
  3. List<Node> nodes = document.selectNodes("/options/category/option");
  4. for (Node node : nodes) {
  5. System.out.println("caption: " + node.selectSingleNode("control/caption").getText());
  6. System.out.println("value : " + node.selectSingleNode("value").getText());
  7. }

代码示例来源:origin: spotbugs/spotbugs

  1. List<Node> componentNodeList = XMLUtil.selectNodes(pluginDescriptor, "/FindbugsPlugin/PluginComponent");
  2. for (Node componentNode : componentNodeList) {
  3. @DottedClassName String componentKindname = componentNode.valueOf("@componentKind");
  4. if (componentKindname == null) {
  5. throw new PluginException("Missing @componentKind for " + plugin.getPluginId()
  6. + " loaded from " + loadedFrom);
  7. @DottedClassName String componentClassname = componentNode.valueOf("@componentClass");
  8. if (componentClassname == null) {
  9. throw new PluginException("Missing @componentClassname for " + plugin.getPluginId()
  10. + " loaded from " + loadedFrom);
  11. String componentId = componentNode.valueOf("@id");
  12. if (componentId == null) {
  13. throw new PluginException("Missing @id for " + plugin.getPluginId()
  14. String propertiesLocation = componentNode.valueOf("@properties");
  15. boolean disabled = Boolean.valueOf(componentNode.valueOf("@disabled"));
  16. String key = node.valueOf("@key");
  17. String value = node.getText();
  18. properties.setProperty(key, value);
  19. List<Node> findBugsMainList = XMLUtil.selectNodes(pluginDescriptor, "/FindbugsPlugin/FindBugsMain");
  20. for (Node main : findBugsMainList) {
  21. String className = main.valueOf("@class");
  22. if (className == null) {
  23. throw new PluginException("Missing @class for FindBugsMain in plugin" + plugin.getPluginId()
  24. + " loaded from " + loadedFrom);

代码示例来源:origin: dom4j/dom4j

  1. public void testBug926713() throws Exception {
  2. Document doc = getDocument("/xml/test/cdata.xml");
  3. Element foo = doc.getRootElement();
  4. Element bar = foo.element("bar");
  5. List content = bar.content();
  6. assertEquals(3, content.size());
  7. assertEquals(Node.TEXT_NODE, ((Node) content.get(0)).getNodeType());
  8. assertEquals(Node.CDATA_SECTION_NODE, ((Node) content.get(1))
  9. .getNodeType());
  10. assertEquals(Node.TEXT_NODE, ((Node) content.get(2)).getNodeType());
  11. }
  12. }

代码示例来源:origin: dom4j/dom4j

  1. public void testSetText1() throws Exception {
  2. String newURL = "newURL";
  3. Node urlNode = document.selectSingleNode("//root/author[1]/url");
  4. urlNode.setText(newURL);
  5. assertEquals(newURL, urlNode.getText());
  6. assertTrue(urlNode instanceof Element);
  7. Element urlElement = (Element) urlNode;
  8. assertEquals(0, urlElement.elements().size());
  9. }

代码示例来源:origin: jboss.jboss-embeddable-ejb3/hibernate-all

  1. protected static void replaceNode(Node container, Element value) {
  2. if ( container!=value ) { //not really necessary, I guess...
  3. Element parent = container.getParent();
  4. container.detach();
  5. value.setName( container.getName() );
  6. value.detach();
  7. parent.add(value);
  8. }
  9. }

代码示例来源:origin: com.github.becausetesting/commons

  1. public void setNodeValue(String xpath, String value) {
  2. Element rootElement = document.getRootElement();
  3. String namespace = rootElement.getNamespaceURI();
  4. if (namespace != null) {
  5. DefaultXPath defaultXPath = new DefaultXPath(xpath);
  6. Map<String, String> namespaces = new TreeMap<String, String>();
  7. namespaces.put("ns", namespace);
  8. defaultXPath.setNamespaceURIs(namespaces);
  9. defaultXPath.selectSingleNode(document).setText(value);
  10. } else {
  11. rootElement.selectSingleNode(xpath).setText(value);
  12. }
  13. }

代码示例来源:origin: org.dom4j/dom4j

  1. public static DefaultNamespaceContext create(Object node) {
  2. Element element = null;
  3. if (node instanceof Element) {
  4. element = (Element) node;
  5. } else if (node instanceof Document) {
  6. Document doc = (Document) node;
  7. element = doc.getRootElement();
  8. } else if (node instanceof Node) {
  9. element = ((Node) node).getParent();
  10. }
  11. if (element != null) {
  12. return new DefaultNamespaceContext(element);
  13. }
  14. return null;
  15. }

代码示例来源:origin: com.atlassian.bamboo.plugins.dotnet/atlassian-bamboo-plugin-dotnet

  1. @NotNull
  2. private static Map<String, Node> cacheUnitTestNodes(final Node document, final String namespacePrefix)
  3. {
  4. final Map<String, Node> cachedTestInfo = new HashMap<>();
  5. final List<Element> cachedNodes = document.selectNodes("//" + namespacePrefix + "UnitTest");
  6. for (final Element e : cachedNodes)
  7. {
  8. cachedTestInfo.put(e.attributeValue("id"), e);
  9. }
  10. return cachedTestInfo;
  11. }

代码示例来源:origin: pentaho/pentaho-kettle

  1. Element e = (Element) node;
  2. List<Attribute> lista = e.attributes();
  3. for ( int i = 0; i < lista.size(); i++ ) {
  4. setAttributeField( lista.get( i ), monitor );
  5. String nodename = node.getName();
  6. String nodenametxt = cleanString( node.getPath() );
  7. String valueNode = node.getText();

代码示例来源:origin: USPTO/PatentPublicData

  1. public NameOrg getOrgName(Node node) {
  2. Node orgNameN = node.selectSingleNode("organization-name");
  3. NameOrg name = orgNameN != null ? new NameOrg(orgNameN.getText()) : null;
  4. if (name != null) {
  5. try {
  6. name.validate();
  7. } catch (InvalidDataException e) {
  8. LOGGER.warn("Org Name Invalid: {}", node.getParent().asXML(), e);
  9. }
  10. }
  11. return name;
  12. }

代码示例来源:origin: USPTO/PatentPublicData

  1. public Examiner getExaminer(Node examinerNode, ExaminerType type) {
  2. if (examinerNode == null) {
  3. return null;
  4. }
  5. Node dataNode = examinerNode.selectSingleNode("PARTY-US");
  6. Name name = new NameNode(dataNode).read();
  7. Node artUnitN = examinerNode.getParent().selectSingleNode("B748US");
  8. String artUnit = artUnitN != null ? artUnitN.getText() : null;
  9. Examiner examiner = new Examiner(name, artUnit, type);
  10. return examiner;
  11. }

代码示例来源:origin: spotbugs/spotbugs

  1. private static String findMessageText(List<Document> messageCollectionList, String xpath, String missingMsg) {
  2. for (Document document : messageCollectionList) {
  3. Node node = document.selectSingleNode(xpath);
  4. if (node != null) {
  5. return node.getText().trim();
  6. }
  7. }
  8. return missingMsg;
  9. }

相关文章