Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewrite options in Context.xml file using Spring

Tags:

java

spring

xml

I have Spring configuration in my project. In that context.xml is dynamically rewritten by me in Java. My question is, why the beans namespace URL is not coming after the file is rewritten?

My context.xml file before rewrite:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2..xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
<!-- <context:annotation-config /> -->

<bean class="org.springframework.ws.client.core.WebServiceTemplate" id="webServiceTemplate">
    <constructor-arg ref="messageFactory"/>
    <property name="marshaller" ref="xmlbeansMarshaller"/>
    <property name="unmarshaller" ref="xmlbeansMarshaller"/>
    <property name="defaultUri">
     <value>https://google.com</value></property>
</bean></beans>


My Java code to rewrite the context.xml:

DocumentBuilderFactory docFactory1 = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder1 = docFactory1.newDocumentBuilder();
Document doc1 = docBuilder1.parse(afilePath);

Node incIncident1 = doc1.getElementsByTagName("beans").item(0);

NodeList beanList = incIncident1.getChildNodes();

NodeList beanlist1 = beanList.item(25).getChildNodes();
List <Map<String, String>> aunitDetails = be.extendedData.get("uicdsDetails");
if (aunitDetails != null) {
    for (int i = 0; i < aunitDetails.size(); i++) {
        Map<String, String> unitLogDetails = aunitDetails.get(i);
        NodeList beanList2= beanlist1.item(7).getChildNodes();
        if (unitLogDetails.get("uURL") != null) {
            beanList2.item(0).setTextContent(unitLogDetails.get("uicdsURL"));
        } else {
            beanList2.item(0).setTextContent("https://google.com");
        }
        TransformerFactory transformerFactory1 = TransformerFactory.newInstance();
        Transformer transformer1 = transformerFactory1.newTransformer();
        System.out.println(doc);
        DOMSource source1 = new DOMSource(doc1);
        StreamResult result1 = new StreamResult(new File(afilePath));
        transformer1.transform(source1, result1);
    }
}


After context.xml is rewritten:

<?xml version="1.0" encoding="UTF-8"?>
    <beans  
        xmlns:context="http://www.springframework.org/schema/context" 
        xmlns:util="http://www.springframework.org/schema/util" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2..xsd 
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
    <!-- <context:annotation-config /> -->

    <bean class="org.springframework.ws.client.core.WebServiceTemplate" id="webServiceTemplate">

        <constructor-arg ref="messageFactory"/>
        <property name="marshaller" ref="xmlbeansMarshaller"/>
        <property name="unmarshaller" ref="xmlbeansMarshaller"/>
        <property name="defaultUri">
         <value>https://google.com</value></property>
    </bean>

</beans>


Here the rewritten context.xml file is missing the XML namespace

xmlns="http://www.springframework.org/schema/beans"

Why this xmlns is missing while rewriting?

like image 240
MadTech Avatar asked May 11 '13 06:05

MadTech


2 Answers

Was a long time ago when I played with DOM, but try docFactory1.setNamespaceAware(true) (it is false by default) or setAttributeNS("http://www.springframework.org/schema/beans", "xmlns").

Btw in order to get help, try to reduce your problem to the bare minimum. Your issue here is with using the Java DOM framework, it has nothing to do with Spring. You could have asked the question in 3 lines w/o all that noise.

like image 83
rlegendi Avatar answered Oct 13 '22 23:10

rlegendi


Just thinking out loud, what could be a a requirement where you would rewrite the xml file with the above code.

Looking at your code, it seems you want to update a property of a bean. Cant you simply get the bean from the context, update its property based on your business logic and do a context refresh! That should keep it simple and save you from the complex thing u r doing.

like image 1
hellojava Avatar answered Oct 13 '22 23:10

hellojava