Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse XML with nested xml opening tags <?xml ...?> in java

can you help me in parsing xml with nested <?xml version="1.0" encoding="utf-8"?> tags. when i am trying to parse this xml, i m getting parsing error.

<?xml version="1.0" encoding="utf-8"?>      
<soap>
            <soapenvBody>
                <serviceResponse>
                    <?xml version="1.0" encoding="UTF-8"?>
                    <data>
                        <respCode>0</respCode>
                    </data>
                </serviceResponse>
            </soapenvBody>
        </soap>  
like image 704
Yogesh Patil Avatar asked Apr 18 '26 00:04

Yogesh Patil


1 Answers

I don't think this is really a Java problem. Having a second XML declaration within the XML body is just illegal, so I don't think you'll be able to get any XML parsers to parse that. If you have control over the XML (it looks like you're generating it to store a response) then you could try wrapping the inner-XML document with CDATA:

<?xml version="1.0" encoding="utf-8"?>     
<soap>
    <soapenvBody>
        <serviceResponse>
          <![CDATA[
              <?xml version="1.0" encoding="UTF-8"?>
              <data>
                  <respCode>0</respCode>
              </data>
          ]]>
        </serviceResponse>
    </soapenvBody>
</soap>

EDIT:

I'm thinking that you most likely don't want the extra XML declaration inside that response at all. Do you have control over the code that creates the response? My guess is that the XML snippet <data>...</data> is created as a separate DOM object and then the string is spliced in the middle of the response. Writing out the entire XML document object results in the XML declaration being included, but if you just grab the document root node object (<data>) and write that out as a string then it probably won't include the extra XML declaration that's causing you all this trouble.

like image 158
DaoWen Avatar answered Apr 20 '26 14:04

DaoWen