i want to insert a formula into doc,but an exception is thrown,
- Code: Select all
javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.SAXException2: unable to marshal type "org.docx4j.math.CTOMathPara" as an element because it is missing an @XmlRootElement annotation]
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:328)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:254)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:75)
at org.docx4j.openpackaging.parts.JaxbXmlPart.marshal(JaxbXmlPart.java:192)
at org.docx4j.openpackaging.parts.JaxbXmlPart.marshal(JaxbXmlPart.java:170)
at org.docx4j.openpackaging.io.SaveToZipFile.saveRawXmlPart(SaveToZipFile.java:201)
at org.docx4j.openpackaging.io.SaveToZipFile.saveRawXmlPart(SaveToZipFile.java:189)
at org.docx4j.openpackaging.io.SaveToZipFile.savePart(SaveToZipFile.java:366)
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:329)
at org.docx4j.openpackaging.io.SaveToZipFile.save(SaveToZipFile.java:159)
at org.docx4j.openpackaging.io.SaveToZipFile.save(SaveToZipFile.java:100)
at org.docx4j.openpackaging.packages.WordprocessingMLPackage.save(WordprocessingMLPackage.java:205)
at org.TestMathFormulaXml.main(TestMathFormulaXml.java:157)
[/code]
my code is
- Code: Select all
package org;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.annotation.XmlRootElement;
import org.docx4j.math.CTOMath;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
public class TestMathFormulaXml
{
private static org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory();
public static JAXBContext context = org.docx4j.jaxb.Context.jc;
public static void insertSubSuper(String key,MainDocumentPart documentPart,WordprocessingMLPackage wordMLPackage)throws Exception
{
String xpath = "//w:r[w:t[contains(text(),'"+key+"')]]";
List<Object> list = documentPart.getJAXBNodesViaXPath(xpath, false);
for(int i=0;i<list.size();i++)
{
org.docx4j.wml.R r = (org.docx4j.wml.R) list.get(i);
org.docx4j.wml.P parent = (org.docx4j.wml.P) r.getParent();
int index =parent.getContent().indexOf(r);
System.out.println(index);
parent.getContent().remove(r);
org.docx4j.math.ObjectFactory mathFactory=new org.docx4j.math.ObjectFactory();
org.docx4j.math.CTOMathPara para=mathFactory.createCTOMathPara();
CTOMath oMath=mathFactory.createCTOMath();
String mr="<m:r xmlns:ve=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" " +
"xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" " +
"xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" " +
"xmlns:v=\"urn:schemas-microsoft-com:vml\" " +
"xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" " +
"xmlns:w10=\"urn:schemas-microsoft-com:office:word\" " +
"xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" " +
"xmlns:wne=\"http://schemas.microsoft.com/office/word/2006/wordml\">" +
"<m:rPr>" +
"<m:sty m:val=\"p\" /> " +
"</m:rPr>" +
"<w:rPr>" +
"<w:rFonts w:ascii=\"Cambria Math\" w:hAnsi=\"Cambria Math\" /> " +
"<w:sz w:val=\"30\" /> " +
"<w:szCs w:val=\"30\" /> " +
"</w:rPr>" +
"<m:t>${mainDesc}</m:t> " +
"</m:r>";
String mpr=" <m:mPr xmlns:ve=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" " +
"xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" " +
"xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" " +
"xmlns:v=\"urn:schemas-microsoft-com:vml\" " +
"xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" " +
"xmlns:w10=\"urn:schemas-microsoft-com:office:word\" " +
"xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" " +
"xmlns:wne=\"http://schemas.microsoft.com/office/word/2006/wordml\">" +
"<m:mcs>" +
"<m:mc>" +
"<m:mcPr>" +
"<m:count m:val=\"1\" /> " +
"<m:mcJc m:val=\"center\" /> " +
"</m:mcPr>" +
"</m:mc>" +
"</m:mcs>" +
"<m:ctrlPr>" +
"<w:rPr>" +
"<w:rFonts w:ascii=\"Cambria Math\" w:hAnsi=\"Cambria Math\" /> " +
"<w:sz w:val=\"30\" />" +
"<w:szCs w:val=\"30\" /> " +
"</w:rPr>" +
"</m:ctrlPr>" +
"</m:mPr>";
String mmr1=retMmr("2");
String mmr2=retMmr("3");
HashMap<String, String> mappings = new HashMap<String, String>();
mappings.put("mainDesc", "C");
Object mrObj=org.docx4j.XmlUtils.unmarshallFromTemplate(mr, mappings,context,org.docx4j.math.CTR.class);
oMath.getEGOMathElements().add((org.docx4j.math.CTR)mrObj);
Object mprObj=org.docx4j.XmlUtils.unmarshalString(mpr,context,org.docx4j.math.CTMPr.class);
Object mmr1Obj=org.docx4j.XmlUtils.unmarshalString(mmr1,context,org.docx4j.math.CTMR.class);
Object mmr2Obj=org.docx4j.XmlUtils.unmarshalString(mmr2,context,org.docx4j.math.CTMR.class);
org.docx4j.math.CTM ctm=mathFactory.createCTM();
ctm.setMPr((org.docx4j.math.CTMPr)mprObj);
ctm.getMr().add((org.docx4j.math.CTMR)mmr1Obj);
ctm.getMr().add((org.docx4j.math.CTMR)mmr2Obj);
oMath.getEGOMathElements().add(ctm);
para.getOMath().add(oMath);
parent.getContent().add(para);
}
}
public static String retMmr(String val)
{
String mmr="<m:mr xmlns:ve=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" " +
"xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" " +
"xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" " +
"xmlns:v=\"urn:schemas-microsoft-com:vml\" " +
"xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" " +
"xmlns:w10=\"urn:schemas-microsoft-com:office:word\" " +
"xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" " +
"xmlns:wne=\"http://schemas.microsoft.com/office/word/2006/wordml\">" +
"<m:e>" +
"<m:r>" +
"<m:rPr>" +
"<m:sty m:val=\"p\" />" +
"</m:rPr>" +
"<w:rPr>" +
"<w:rFonts w:ascii=\"Cambria Math\" w:hAnsi=\"Cambria Math\" />" +
"<w:sz w:val=\"30\" />" +
"<w:szCs w:val=\"30\" />" +
"</w:rPr>" +
"<m:t>"+val+"</m:t>" +
"</m:r>" +
"</m:e>" +
"</m:mr>";
return mmr;
}
public static void main(String[] args)
{
try
{
String inputfilepath = "c:/workspace/aa.docx";
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
.load(new java.io.File(inputfilepath));
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
insertSubSuper("label2",documentPart,wordMLPackage);
wordMLPackage.save(new File("c:/workspace/aa_out.docx"));
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
thanks in advance!
tosswang