Groovy: how to parse xml and save namespaces and schemaLocations

I am trying to use groovy to just add node in in a specific place. My original schema looks like this:

<s1:RootNode
   xmlns:s1="http://localhost/s1schema"
   xmlns:s2="http://localhost/s2schema"
   xsi:schemaLocation="http://localhost/s1schema s1schema.xsd 
   http://localhost/s2schema s2schema.xsd">
 <s1:aParentNode>
  <s2:targetNode>
   <s2:childnode1 />
   <s2:childnode2 />
   <s2:childnode3 />
   <s2:childnode4 />
 </s2:targetNode>
</s1:aParentNode>
</s1:RootNode>

I would just add a new child node inline with others to make a conclusion

<s1:RootNode
    xmlns:s1="http://localhost/s1schema"
    xmlns:s2="http://localhost/s2schema"
    xsi:schemaLocation="http://localhost/s1schema s1schema.xsd 
    http://localhost/s2schema s2schema.xsd">
 <s1:aParentNode>    
   <s2:targetNode>
     <s2:childnode1 />
     <s2:childnode2 />
     <s2:childnode3 />
     <s2:childnode4 />
     <s2:childnode5 >value</s2:childnode5>
   </s2:targetNode>
  </s1:aParentNode>
 </s1:RootNode>

For this, I have the following simple groovy script

  def data = 'value'
def root = new XmlSlurper(false,true).parseText( sourceXML )
        root.'aParentNode'.'topNode'.appendNode{
            's2:childnode5' data
        }
groovy.xml.XmlUtil.serialize(root);

however, when I do this, the namespaces and Locations schemas that apply to the root of the node are deleted. and a namespace, but no schema location is added to each of the child nodes.

this causes downstream verification problems.

How to simply process this xml. don't check and leave the xml as is and add one node of the namespace, which will I indicate?

: , (s1 ), , " " xml

!

+5
2

-, xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance", xsi. SAXParseException xsi.

, question xpace node .

, StreamingMarkupBuilder . , node, . s2 "targetNode". , , StreamingMarkupBuilder.

 def root = new XmlSlurper(false, true).parseText( sourceXML )
 def data = '<s2:childnode5 xmlns:s2="http://localhost/s2schema">value</s2:childnode5>'
 def xmlFragment = new XmlSlurper(false, true).parseText(data)
 root.'aParentNode'.'targetNode'.appendNode(xmlFragment);

 def outputBuilder = new StreamingMarkupBuilder()
 String result = XmlUtil.serialize(outputBuilder.bind {
     mkp.declareNamespace('s1':"http://localhost/s1schema")
     mkp.declareNamespace('s2':"http://localhost/s2schema")
     mkp.yield root }
 )
+2

XMLSlurper ( XMLParser) , :

XmlSlurper (boolean validating, boolean namespaceAware)

false:

def root = new XmlSlurper(false, false).parseText( sourceXML )

namespaceAware false, bahavior . false, XML , .

0

All Articles