使用xmlstarlet在另一个元素后直接插入元素不起作用

时间:2015-11-13 12:12:52

标签: xml xml-parsing xmlstarlet

我知道这些问题已被多次回答,但它们都不适用于我,至少......使用我的XML文件。我用更简单的XML树尝试了这些答案,然而,他们的工作方式与我的不同。这就是我发布一个希望有人启发我的问题的原因: - )

我的XML(Liferay的东西)如下:

<?xml version="1.0"?>

<beans>
    <bean id="liferayDataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
        <property name="targetDataSource">
            <bean class="org.springframework.aop.framework.ProxyFactoryBean">
                <property name="targetSource" ref="shardDataSourceTargetSource" />
            </bean>
        </property>
    </bean>

    <bean id="liferayHibernateSessionFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="targetSource" ref="shardSessionFactoryTargetSource" />
    </bean>

    <bean id="shardDataSource0" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
        <property name="targetDataSource">
            <bean class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean">
                <property name="propertyPrefix" value="jdbc.default." />
            </bean>
        </property>
    </bean>

    <bean id="shardDataSource1" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
        <property name="targetDataSource">
            <bean class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean">
                <property name="propertyPrefix" value="jdbc.NIGERIA." />
            </bean>
        </property>
    </bean>

    <bean id="shardDataSource2" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
        <property name="targetDataSource">
            <bean class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean">
                <property name="propertyPrefix" value="jdbc.MEDLAHO." />
            </bean>
        </property>
    </bean>

    <bean id="shardDataSourceTargetSource" class="com.liferay.portal.dao.shard.ShardDataSourceTargetSource">    
        <property name="dataSources">
            <map>
                <entry>
                    <key>
                        <value>default</value>
                    </key>
                    <ref bean="shardDataSource0" />
                </entry>
                <entry>
                    <key>
                        <value>NIGERIA</value>
                    </key>
                    <ref bean="shardDataSource1" />
                </entry>
                <entry>
                    <key>
                        <value>MEDLAHO</value>
                    </key>
                    <ref bean="shardDataSource2" />
                </entry>
            </map>
        </property>
    </bean>
    <bean id="shardSessionFactoryTargetSource" class="com.liferay.portal.dao.shard.ShardSessionFactoryTargetSource">
        <property name="shardDataSourceTargetSource" ref="shardDataSourceTargetSource" />
    </bean>
    <bean id="com.liferay.portal.kernel.dao.jdbc.MappingSqlQueryFactoryUtil" class="com.liferay.portal.kernel.dao.jdbc.MappingSqlQueryFactoryUtil">
        <property name="mappingSqlQueryFactory">
            <bean class="com.liferay.portal.dao.shard.ShardMappingSqlQueryFactoryImpl" />
        </property>
    </bean>
    <bean id="com.liferay.portal.kernel.dao.jdbc.SqlUpdateFactoryUtil" class="com.liferay.portal.kernel.dao.jdbc.SqlUpdateFactoryUtil">
        <property name="sqlUpdateFactory">
            <bean class="com.liferay.portal.dao.shard.ShardSqlUpdateFactoryImpl" />
        </property>
    </bean>
    <bean id="com.liferay.portal.kernel.dao.shard.ShardUtil" class="com.liferay.portal.kernel.dao.shard.ShardUtil">
        <property name="shard">
            <bean class="com.liferay.portal.dao.shard.ShardImpl">
                <property name="shardAdvice" ref="com.liferay.portal.dao.shard.advice.ShardAdvice" />
            </bean>
        </property>
    </bean>
    <bean id="com.liferay.portal.kernel.util.InfrastructureUtil" class="com.liferay.portal.kernel.util.InfrastructureUtil">
        <property name="dataSource" ref="liferayDataSource" />
        <property name="mailSession" ref="mailSession" />
        <property name="shardDataSourceTargetSource" ref="shardDataSourceTargetSource" />
        <property name="shardSessionFactoryTargetSource" ref="shardSessionFactoryTargetSource" />
        <property name="transactionManager" ref="liferayTransactionManager" />
    </bean>
    <bean id="com.liferay.portal.dao.shard.advice.ShardAdvice" class="com.liferay.portal.dao.shard.advice.ShardAdvice">
        <property name="shardDataSourceTargetSource" ref="shardDataSourceTargetSource" />
        <property name="shardSessionFactoryTargetSource" ref="shardSessionFactoryTargetSource" />
    </bean>
    <bean id="com.liferay.portal.dao.shard.advice.ShardCompanyAdvice" class="com.liferay.portal.dao.shard.advice.ShardCompanyAdvice">
        <property name="shardAdvice" ref="com.liferay.portal.dao.shard.advice.ShardAdvice" />
    </bean>
    <bean id="com.liferay.portal.dao.shard.advice.ShardPortletAdvice" class="com.liferay.portal.dao.shard.advice.ShardPortletAdvice">
        <property name="shardAdvice" ref="com.liferay.portal.dao.shard.advice.ShardAdvice" />
    </bean>
    <bean id="com.liferay.portal.dao.shard.advice.ShardPersistenceAdvice" class="com.liferay.portal.dao.shard.advice.ShardPersistenceAdvice">
        <property name="shardAdvice" ref="com.liferay.portal.dao.shard.advice.ShardAdvice" />
    </bean>
    <bean id="com.liferay.portal.dao.shard.advice.ShardGloballyAdvice" class="com.liferay.portal.dao.shard.advice.ShardGloballyAdvice">
        <property name="shardAdvice" ref="com.liferay.portal.dao.shard.advice.ShardAdvice" />
    </bean>
    <bean id="com.liferay.portal.dao.shard.advice.ShardIterativelyAdvice" class="com.liferay.portal.dao.shard.advice.ShardIterativelyAdvice">
        <property name="shardAdvice" ref="com.liferay.portal.dao.shard.advice.ShardAdvice" />
    </bean>
    <bean id="com.liferay.portal.dao.shard.advice.ShardParameterAdvice" class="com.liferay.portal.dao.shard.advice.ShardParameterAdvice">
        <property name="shardAdvice" ref="com.liferay.portal.dao.shard.advice.ShardAdvice" />
    </bean>
    <aop:config proxy-target-class="false">
        <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardCompanyAdvice" pointcut="bean(com.liferay.portal.service.CompanyLocalService)" />
        <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardGloballyAdvice" pointcut="execution(void com.liferay.portal.convert.messaging.ConvertProcessMessageListener.receive(..))" />
        <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardGloballyAdvice" pointcut="execution(void com.liferay.portal.events.StartupHelper.updateIndexes())" />
        <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardGloballyAdvice" pointcut="execution(void com.liferay.portal.events.StartupHelper.upgradeProcess(int))" />
        <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardGloballyAdvice" pointcut="execution(* com.liferay.portal.service.ReleaseLocalService.getBuildNumberOrCreate())" />
        <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardGloballyAdvice" pointcut="execution(void com.liferay.portal.service.ServiceComponentLocalService.upgradeDB(..))" />
        <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardGloballyAdvice" pointcut="execution(void com.liferay.portlet.journal.service.JournalArticleLocalService.checkArticles())" />
        <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardIterativelyAdvice" pointcut="execution(void com.liferay.portal.events.StartupHelper.verifyProcess(boolean, boolean))" />
        <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardParameterAdvice" pointcut="execution(* com.liferay.portal.service.AccountLocalService.getAccount(long, long))" />
        <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardParameterAdvice" pointcut="execution(* com.liferay.portal.service.GroupLocalService.getCompanyGroup(long))" />
        <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardParameterAdvice" pointcut="execution(* com.liferay.portal.service.UserLocalService.searchCount(..))" />
        <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardPersistenceAdvice" pointcut="bean(*Persistence) || bean(*Finder)" />
        <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardPortletAdvice" pointcut="bean(com.liferay.portal.service.PortletLocalService)" />
    </aop:config>
</beans>

我想要的是像块一样有另一个 bean ,例如:

    <bean id="shardDataSource4" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
        <property name="targetDataSource">
            <bean class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean">
                <property name="propertyPrefix" value="jdbc.default." />
            </bean>
        </property>
    </bean>

id = shardDataSource0

之后

我没有尝试插入包含所有子元素及其属性的完整块,我只是尝试为一个开始插入一个新的 bean 元素...但我不能。

我尝试了很多选项,但它不能像这一样简单:

xmlstarlet ed -L -a /beans/bean[@id='shardDataSource0'] -t elem -n bean -v "" file.xml

哪个不起作用:-(它什么都不做,文件没有被修改,退出代码是0。

所以......我在这里想念的是什么?

很多,非常感谢提前。

注意:我正在运行SLES扩展支持平台版本6.7,而xmlstarlet包是:xmlstarlet-1.5.0-1.el6.rf.x86_64

更新

感谢@npostavs的回答,我设法追加整个 bean 块。正确的行如下:

xmlstarlet ed -a "/beans/bean[@id='shardDataSource0']" -t elem -n beanTMP -v ""  \
    -i //beanTMP -t attr -n "id" -v "shardDataSource1" \
    -i //beanTMP -t attr -n "class" -v "org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy" \
    -s //beanTMP -t elem -n "propertyTMP" -v "" \
    -i //propertyTMP -t attr -n "name" -v "targetDataSource" \
    -s //propertyTMP -t elem -n "beanTMP2" -v "" \
    -i //beanTMP2 -t attr -n "class" -v "com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean" \
    -s //beanTMP2 -t elem -n "propertyTMP2" -i //propertyTMP2 -t attr -n "name" -v "propertyPrefix" \
    -i //propertyTMP2 -t attr -n "value" -v "jdbc.default." \
    -r //propertyTMP -v "property" \
    -r //propertyTMP2 -v "property" \
    -r //beanTMP -v "bean" \
    -r //beanTMP2 -v "bean" \
    file.xml

然而,结果如下:

<?xml version="1.0"?>
<beans>
  <bean id="shardDataSource0" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
    <property name="targetDataSource">
      <bean class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean">
        <property name="propertyPrefix" value="jdbc.default."/>
      </bean>
    </property>
  </bean>
  <bean id="shardDataSource1" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy"><property name="targetDataSource"><bean class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean"><property name="propertyPrefix" value="jdbc.default."/></bean></property></bean>
</beans>

这意味着我没有得到正确的格式。我尝试了全局选项-P -S和--ps或--pf,但它们都不起作用.... 任何人都可以知道为什么会这样吗?

非常感谢所有人提前!!

更新2

我设法将第一个xmlstarlet行正确格式化为: xmlstarlet fo -R 格式化整个文档。但我相信这不是要走的路......所以,伙计们,......任何线索?

0 个答案:

没有答案