2013-05-11 8 views
9

私のプロジェクトにはSpringの設定があります。その中でcontext.xmlはJavaで私によって動的に書き換えられます。私の質問は、ファイルが書き換えられた後、なぜ豆の名前空間のURLが来ていないのですか?Springを使用したContext.xmlファイルのリライトオプション

書き換え前に私のの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); 
    } 
} 


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


私のJavaコード。 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> 


書き換えながらこのxmlnsが欠落しているのはなぜここに書き換えのcontext.xmlファイルが

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

XML名前空間が不足していますか?

答えて

3

DOMで遊んだのはずっと前ですが、docFactory1.setNamespaceAware(true)(デフォルトではfalse)またはsetAttributeNS("http://www.springframework.org/schema/beans", "xmlns")です。

Btwヘルプを得るには、問題を最小限に抑えてください。ここでの問題は、Java DOMフレームワークを使用していることです。これはSpringとは関係ありません。あなたは3つのラインで質問をすることができましたが、そのノイズはありません。

+0

ありがとうございました。あなたのコメントに従う次回私はあなたのコメントに従います – MadTech

+0

私は、仲間、違反しないでください:-)それは100行で同じよりも3行のコードで問題を理解することは簡単に簡単です;]とSOで歓迎 - btw、これらのソリューションは助けましたか? – rlegendi

+0

@MadTech:プラスあなたを助けてくれた人、お願いします。 – ron

1

xmlファイルを上記のコードで書き直す必要があります。

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

関連する問題