Dom4J读取指定标签内容

xml如下,读取 default标签内容

<?xml version='1.0' encoding='UTF-8'?>
<project>
  <actions/>
  <description></description>
  <logRotator class="hudson.tasks.LogRotator">
    <daysToKeep>-1</daysToKeep>
    <numToKeep>20</numToKeep>
    <artifactDaysToKeep>20</artifactDaysToKeep>
    <artifactNumToKeep>20</artifactNumToKeep>
  </logRotator>
  <keepDependencies>false</keepDependencies>
  <properties>
    <hudson.security.AuthorizationMatrixProperty>
    </hudson.security.AuthorizationMatrixProperty>
    <hudson.model.ParametersDefinitionProperty>
      <parameterDefinitions>
        <hudson.model.TextParameterDefinition>
          <name>globalizationinfo</name>
          <description></description>
          <defaultValue>{&quot;checkType&quot;: [&quot;checkSpell&quot;, &quot;checkStandardization&quot;, &quot;checkMixtureCharOfTarget&quot;],&quot;defaultLocaleName&quot;: &quot;values&quot;,&quot;defaultResourse&quot;: {&quot;langTpye&quot;: &quot;values&quot;,&quot;resourcepath&quot;: [&quot;res\\values\\strings.xml&quot;]},&quot;productType&quot;: &quot;android&quot;,&quot;targetResourse&quot;: [{&quot;langTpye&quot;: &quot;values-fr&quot;,&quot;resourcepath&quot;: [&quot;res\\values-fr\\strings.xml&quot;, &quot;res\\values-fr\\config.xml&quot;]},{&quot;langTpye&quot;: &quot;values-ar&quot;,&quot;resourcepath&quot;: [&quot;res\\values-ar\\strings.xml&quot;]}]}
</defaultValue>
        </hudson.model.TextParameterDefinition>
      </parameterDefinitions>
    </hudson.model.ParametersDefinitionProperty>
    <com.sonyericsson.rebuild.RebuildSettings plugin="[email protected]">
      <autoRebuild>false</autoRebuild>
    </com.sonyericsson.rebuild.RebuildSettings>
    <com.jenkins.plugins.PbiInfo plugin="[email protected]">
      <productline></productline>
      <productname></productname>
      <productversionr></productversionr>
      <productversionc></productversionc>
      <baselineversion></baselineversion>
      <team></team>
      <checkField>false</checkField>
      <pbiID></pbiID>
      <jobPbiInfo></jobPbiInfo>
    </com.jenkins.plugins.PbiInfo>
  </properties>
  
</project>

java代码如下 

String pramJson = null;
		//解析xml
		Document document = null;
		Element root = null;
		Element childEle = null;
	    SAXReader reader=null;
		boolean findPram ;
		// CODEWS:在处理的时候,添加上服务器根路径;
		FileInputStream xmlInputStream=null;
		try {
			//获取dom对象
	        reader = new SAXReader();
	        document = reader.read(configPath);
	        root = document.getRootElement();
			// 获取pramJson
			List<Element> textValueList = root.selectNodes("/project/properties/hudson.model.ParametersDefinitionProperty/parameterDefinitions/hudson.model.TextParameterDefinition");
			for (Element e : textValueList) {
				if (e.elementText("name").equals("globalizationinfo")) {
					pramJson = e.elementText("defaultValue");
				}
			}
	        p=JSONObject.parseObject(pramJson, ParamBean.class);
	        String pluginId=ProductTypeEnum.getPluginId(p.getProductType());
			if(StringUtils.isEmpty(pluginId)) {
				log.error("产品类型不支持:"+p.getProductType());
				return null;
			}
			p.setPluginId(pluginId);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			log.error("获取参数失败");
		}finally {
			try {
				if(null != xmlInputStream){
					xmlInputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

猜你喜欢

转载自blog.csdn.net/weixin_41796956/article/details/81258228