9

Springプロジェクトに構成があります。その中で、context.xmlは Java で動的に書き換えられます。私の質問は、ファイルが書き換えられた後に Bean 名前空間の URL が表示されないのはなぜですか?

書き換える前の私のcontext.xmlファイル:

<?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>


context.xml を書き換える私の Java コード:

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);
    }
}


context.xmlを書き換え た後:

<?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>


ここで、書き換えられたcontext.xmlファイルには XML 名前空間がありませ ん

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

xmlns書き換え中にこれが欠落しているのはなぜですか?

4

2 に答える 2

3

ずいぶん前にDOMで遊んでいたのですが、試しdocFactory1.setNamespaceAware(true)てみてください(falseデフォルトです) setAttributeNS("http://www.springframework.org/schema/beans", "xmlns")

ところで、助けを得るには、問題を最小限に抑えるようにしてください。ここでの問題は、Java DOM フレームワークの使用に関するものであり、Spring とは関係ありません。ノイズがまったくなくても、3 行で質問できたはずです。

于 2013-05-13T06:27:36.087 に答える
1

声に出して考えてみると、上記のコードで xml ファイルを書き換える要件は何でしょうか。

コードを見ると、Bean のプロパティを更新したいようです。コンテキストから Bean を取得し、ビジネス ロジックに基づいてそのプロパティを更新し、コンテキストを更新するだけではいけません。それはそれをシンプルに保ち、あなたがしている複雑なことからあなたを救うはずです.

于 2013-05-19T14:46:39.797 に答える