无法使用xpath和vbscript编辑xml文件

时间:2015-07-01 15:36:12

标签: xml xpath vbscript

我正在尝试使用vbs编辑一个相对较大的xml文件中的一个属性。每当我尝试使用selectSingleNode操作时,我都会收到错误。这是一个缩短的xml文件,应该提供所需的所有信息。我需要将root-logger节点编辑为WARN而不是INFO

<?xml version="1.0" encoding="UTF-8"?>
<server xmlns="urn:jboss:domain:1.2">
<extensions>
    <extension module="org.jboss.as.clustering.infinispan"/>
    <extension module="org.jboss.as.cmp"/>
    <extension module="org.jboss.as.configadmin"/>
    <extension module="org.jboss.as.connector"/>
    <extension module="org.jboss.as.deployment-scanner"/>
    <extension module="org.jboss.as.ee"/>
    <extension module="org.jboss.as.ejb3"/>
    <extension module="org.jboss.as.jacorb"/>
        <extension module="org.jboss.as.jaxr"/>
        <extension module="org.jboss.as.jaxrs"/>
        <extension module="org.jboss.as.jdr"/>
        <extension module="org.jboss.as.jmx"/>
        <extension module="org.jboss.as.jpa"/>
        <extension module="org.jboss.as.jsr77"/>
        <extension module="org.jboss.as.logging"/>
    </extensions>
    <management>
        <security-realms>
            <security-realm name="ManagementRealm">
                <authentication>
                    <properties path="mgmt-users.properties" relative-to="jboss.server.config.dir"/>
                </authentication>
            </security-realm>
            <security-realm name="ApplicationRealm">
                <authentication>
                    <properties path="application-users.properties" relative-to="jboss.server.config.dir"/>
                </authentication>
            </security-realm>
        </security-realms>
        <management-interfaces>
            <native-interface security-realm="ManagementRealm">
                <socket-binding native="management-native"/>
            </native-interface>
            <http-interface security-realm="ManagementRealm">
                <socket-binding http="management-http"/>
            </http-interface>
        </management-interfaces>
    </management>
    <profile>
        <subsystem xmlns="urn:jboss:domain:logging:1.1">
            <logger category="com.arjuna">
                <level name="WARN"/>
            </logger>
            <logger category="org.apache.tomcat.util.modeler">
                <level name="WARN"/>
            </logger>
            <root-logger>
                <level name="INFO"/>
                <handlers>
                    <handler name="CONSOLE"/>
                    <handler name="FILE"/>
                </handlers>
            </root-logger>
        </subsystem>
    </profile>
</server> 

我尝试用来编辑文件的脚本如下所示

set xml = CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.validateOnParse = false
xml.resolveExternals = false
xml.setProperty "SelectionLanguage", "XPath"
xml.setProperty "SelectionNamespaces", "xmlns:m='urn:jboss:domain:logging:1.1'"
slog4j = "WARN"

err.clear
on error resume next
xml.load (WScript.arguments(0))
if (err.number = 0) then
xml.selectSingleNode("//profile/subsystem/root-logger/level/@").text = slog4j
strResult = xml.save(WScript.arguments(0))
end if

它似乎正在正确地读取xml文件并且在获取属性时遇到了xpath位置的问题,但我使用了类似于先前文件的格式。任何建议都会很精彩。抱歉,xml文件的长代码块。我不知道有多少我可以删除,同时仍然提供足够的信息来获得一些帮助

2 个答案:

答案 0 :(得分:1)

您的XML使用两个名称空间,如果要在两个名称空间中选择元素,则必须使用不同的前缀声明它们。所以用

set xml = CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.validateOnParse = false
xml.resolveExternals = false
xml.setProperty "SelectionLanguage", "XPath"
xml.setProperty "SelectionNamespaces", "xmlns:jb='urn:jboss:domain:1.2' xmlns:jl='urn:jboss:domain:logging:1.1'"
slog4j = "WARN"

err.clear
on error resume next
xml.load (WScript.arguments(0))
if (err.number = 0) then
xml.selectSingleNode("//jb:profile/jl:subsystem/jl:root-logger/jl:level/@name").text = slog4j
strResult = xml.save(WScript.arguments(0))
end if

我改变了属性。

答案 1 :(得分:0)

对于XPath,我是一个完整的新手,但您应该能够选择元素本身,然后使用var data= "rows":[ { "STATUS" : "Active", "count" : "246" }, { "STATUS" : "Not Proceeded With", "count" : "40" } ] var width = 800, height = 250, radius = Math.min(width, height) / 2; var color = d3.scale.ordinal() .range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]); var arc = d3.svg.arc() .outerRadius(radius - 10) .innerRadius(radius - 70); var pie = d3.layout.pie() .sort(null) .value(function (d) { return d.count; }); var svg = d3.select("#chart").append("svg") .attr("width", width) .attr("height", height) .append("g") .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")"); var g = svg.selectAll(".arc") .data(pie(data.rows)) .enter().append("g") .attr("class", "arc"); g.append("path") .attr("d", arc) .style("fill", function (d) { return color(d.data.rows.STATUS); }); g.append("text") .attr("transform", function (d) { return "translate(" + arc.centroid(d) + ")"; }) .attr("dy", ".35em") .style("text-anchor", "middle") .text(function (d) { return d.data.rows.STATUS; }); 属性更新属性值:

attributes

编辑:

好的,显然这只能使用XSL Pattern作为选择语言而不是XPath,所以你必须注释掉:

xml.selectSingleNode("//profile/subsystem/root-logger/level").attributes.item(0).text = slog4j