0

I am trying to edit one attribute inside of a relatively large xml file using vbs. I am getting errors wheneverI try to use the selectSingleNode operation I am getting errors. This a shortened xml file that should give all of the information needed. I need to edit the of the root-logger node to be WARN instead of 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> 

the script I am trying to use to edit the file looks like this

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

It seems to be reading in the xml file correctly and having a problem with the xpath location to get to the attribute, but I used a similar format that worked on a previous file. Any suggestions would be wonderful. Sorry for the long block of code for the xml file. I didnt know how much i could remove while still giving enough information to get some help

2 Answers 2

1

Your XML uses two namespaces and you have to declare them both with different prefixes if you want to select elements in both namespaces. So with

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

I get the attribute changed.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried that, but i still get an error on the selectSingleNode call. err.number becomes 424 and doesn't complete the operation
I have looked more closely at your XML sample and it has two namespaces so you need to use two prefixes if you want to use both the profile element in one namespace and the subsystem and its descendant in a second namespace. I have edited my answer with a tested code sample.
Thank you so much. I cant believe I overlooked that. perfect
0

I'm a complete newb when it comes to XPath, but you should be able to select the element itself and then just update the attribute value using the attributes property:

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

Edit:

OK, so apparently this only works using XSL Pattern as the selection language but not XPath, so you'd have to comment out:

xml.setProperty "SelectionLanguage", "XPath"

3 Comments

I get the same error attempting to do that as when I use the @ symbol because from my understanding since it is the only attribute, the @ symbol will get me to the same location as specifically calling the attribute.
In XPath you can use @* to select all attributes or @foo to select the foo attribute but having only @ is a syntax error.
I was able to edit another xml file using only the @ without adding * or foo for a similar operation, but the code I have that works on a different XML file throws a ton of errors on this operation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.