Page 1 of 1

Exception : The prefix "w" for element "w:p" is not bound

PostPosted: Wed Jan 25, 2012 1:42 am
by sureshbabubv
Dear All,

I am trying to add a text in a paragraph and get the code from docx file.

I used this following code in my java application as shown below

Code: Select all
org.docx4j.wml.Pict objWmlPicture = (Pict) XmlUtils.unmarshalString(sXmlData);


Where sXmlData data containts the xml that has added below.

I am getting the following exception " org.xml.sax.SAXParseException: The prefix "w" for element "w:p" is not bound."

Please advice me how to resolve this.

Thanks & Regards,
B.V.Suresh Babu.

============================================================================================================================================
<w:p>
<w:pict>
<v:shapetype id="_x0000_t202" coordsize="21600,21600"
o:spt="202" path="m,l,21600r21600,l21600,xe">
<v:stroke joinstyle="miter" />
<v:path gradientshapeok="t" o:connecttype="rect" />
</v:shapetype>
<v:shape id="Text Box 195" o:spid="_x0000_s1026" type="#_x0000_t202"
style="position:absolute;margin-left:-7.2pt;margin-top:16.45pt;width:2in;height:110pt;z-index:251623936;visibility:visible;mso-wrap-style:square;mso-width-percent:0;mso-height-percent:0;mso-wrap-distance-left:9pt;mso-wrap-distance-top:0;mso-wrap-distance-right:9pt;mso-wrap-distance-bottom:0;mso-position-horizontal:absolute;mso-position-horizontal-relative:text;mso-position-vertical:absolute;mso-position-vertical-relative:text;mso-width-percent:0;mso-height-percent:0;mso-width-relative:page;mso-height-relative:page;v-text-anchor:top"
filled="f" stroked="f">
<v:textbox>
<w:txbxContent>
<w:p w:rsidR="002E57F9" w:rsidRDefault="0030198C">
<w:pPr>
<w:jc w:val="center" />
</w:pPr>
<w:r>
<w:rPr>
<w:sz w:val="28" />
<w:szCs w:val="19" />
</w:rPr>
<w:t>Suresh</w:t>
</w:r>
</w:p>
</w:txbxContent>
</v:textbox>
</v:shape>
</w:pict>

</w:p>

Re: Exception : The prefix "w" for element "w:p" is not boun

PostPosted: Wed Jan 25, 2012 4:46 am
by sureshbabubv
Dear All,

I tried with the following code aslo

Code: Select all
P objP = (P)XmlUtils.unmarshalString(sXmlData,Context.jc,P.class);


but getting the same exceptiion


Code: Select all
org.xml.sax.SAXParseException: The prefix "w" for element "w:p" is not bound.


Please correct me where I am doing the mistake.

Thanks & Regards,
B.V.Suresh Babu.

Re: Exception : The prefix "w" for element "w:p" is not boun

PostPosted: Wed Jan 25, 2012 10:44 am
by jason
You need to add namespace declarations into your strings, one for w:, and another for v:.

Re: Exception : The prefix "w" for element "w:p" is not boun

PostPosted: Thu Jan 26, 2012 12:53 am
by sureshbabubv
Thankyou jason,

I resolved this issue by adding the name spaces in the string as shown below


Code: Select all
String sXmlData =       "<w:pict xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:v=\"urn:schemas-microsoft-com:vml\"><v:shapetype id=\"_x0000_t202\" coordsize=\"21600,21600\" o:spt=\"202\" path=\"m,l,21600r21600,l21600,xe\"><v:stroke joinstyle=\"miter\"/><v:path gradientshapeok=\"t\" o:connecttype=\"rect\"/></v:shapetype><v:shape id=\"Text Box 195\" o:spid=\"_x0000_s1026\" type=\"#_x0000_t202\" style=\"position:absolute;margin-left:-7.2pt;margin-top:16.45pt;width:2in;height:110pt;z-index:251623936;visibility:visible;mso-wrap-style:square;mso-width-percent:0;mso-height-percent:0;mso-wrap-distance-left:9pt;mso-wrap-distance-top:0;mso-wrap-distance-right:9pt;mso-wrap-distance-bottom:0;mso-position-horizontal:absolute;mso-position-horizontal-relative:text;mso-position-vertical:absolute;mso-position-vertical-relative:text;mso-width-percent:0;mso-height-percent:0;mso-width-relative:page;mso-height-relative:page;v-text-anchor:top\"  filled=\"f\" stroked=\"f\"><v:textbox><w:txbxContent><w:p w:rsidR=\"002E57F9\" w:rsidRDefault=\"0030198C\"><w:pPr><w:jc w:val=\"center\"/></w:pPr><w:r><w:rPr><w:sz w:val=\"28\"/><w:szCs w:val=\"19\"/></w:rPr><w:t>"+designText+"</w:t></w:r></w:p></w:txbxContent></v:textbox></v:shape></w:pict>";



I would like to share one new thing I learned while working with the above code.

I am passing a dynamic text to the string, I get the
Code: Select all
[org.xml.sax.SAXParseException: The entity name must immediately follow the '&' in the entity reference.]
exception while the text contains "&" symbol.

To resolve that I used the following code

designText = designText.replaceAll("&", "&");


I used the Ascei code of &
and it resolved my issue.

Thanks & Regards,
B.V.Suresh Babu.