Search code examples
javaxml-parsingjaxbsaxparserdocx4j

docx4j - Load part from xml file


I'm trying to create a NumberingDefinitionsPart from a number.xml extracted from another docx file. Here is what I have tried:

String xml = new String( Files.readAllBytes(Paths.get(path)),  Charset.defaultCharset() );
NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
wordMLPack.addTargetPart( ndp );
ndp.setJaxbElement( (Numbering) XmlUtils.unmarshalString( xml ) );

Exception is thrown at the unmarshalString method(Sorry for containing CJK in the exception):

[org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 5357; 与元素类型 "null" 相关联的 "w:val" 属性值不能包含 '<' 字符。]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at org.docx4j.XmlUtils.unmarshalString(XmlUtils.java:433)
at org.docx4j.XmlUtils.unmarshalString(XmlUtils.java:413)
at doc_utils.Demo.loadPartFromXML(Demo.java:289)
at doc_utils.Demo.demo(Demo.java:79)
at doc_utils.Demo.main(Demo.java:57)
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 5357; 与元素类型 "null" 相关联的 "w:val" 属性值不能包含 '<' 字符。
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
... 9 more

Here is the xml pretty printed:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:numbering xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:ns9="http://schemas.openxmlformats.org/schemaLibrary/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:ns12="http://schemas.openxmlformats.org/drawingml/2006/chartDrawing" xmlns:dgm="http://schemas.openxmlformats.org/drawingml/2006/diagram" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:dsp="http://schemas.microsoft.com/office/drawing/2008/diagram" xmlns:ns17="urn:schemas-microsoft-com:office:excel" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:ns21="urn:schemas-microsoft-com:office:powerpoint" xmlns:ns23="http://schemas.microsoft.com/office/2006/coverPageProps" xmlns:odx="http://opendope.org/xpaths" xmlns:odc="http://opendope.org/conditions" xmlns:odq="http://opendope.org/questions" xmlns:oda="http://opendope.org/answers" xmlns:odi="http://opendope.org/components" xmlns:odgm="http://opendope.org/SmartArt/DataHierarchy" xmlns:ns30="http://schemas.openxmlformats.org/officeDocument/2006/bibliography" xmlns:ns31="http://schemas.openxmlformats.org/drawingml/2006/compatibility" xmlns:ns32="http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas">
<w:abstractNum w:abstractNumId="0">
    <w:nsid w:val="054F4697"/>
    <w:multiLevelType w:val="hybridMultilevel"/>
    <w:tmpl w:val="B6660F98"/>
    <w:lvl w:ilvl="0" w:tplc="0409000F">
        <w:start w:val="1"/>
        <w:numFmt w:val="decimal"/>
        <w:lvlText w:val="%1."/>
        <w:lvlJc w:val="left"/>
        <w:pPr>
            <w:ind w:left="420" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="1" w:tplc="04090019" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="lowerLetter"/>
        <w:lvlText w:val="%2)"/>
        <w:lvlJc w:val="left"/>
        <w:pPr>
            <w:ind w:left="840" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="2" w:tplc="0409001B" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="lowerRoman"/>
        <w:lvlText w:val="%3."/>
        <w:lvlJc w:val="right"/>
        <w:pPr>
            <w:ind w:left="1260" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="3" w:tplc="0409000F" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="decimal"/>
        <w:lvlText w:val="%4."/>
        <w:lvlJc w:val="left"/>
        <w:pPr>
            <w:ind w:left="1680" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="4" w:tplc="04090019" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="lowerLetter"/>
        <w:lvlText w:val="%5)"/>
        <w:lvlJc w:val="left"/>
        <w:pPr>
            <w:ind w:left="2100" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="5" w:tplc="0409001B" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="lowerRoman"/>
        <w:lvlText w:val="%6."/>
        <w:lvlJc w:val="right"/>
        <w:pPr>
            <w:ind w:left="2520" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="6" w:tplc="0409000F" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="decimal"/>
        <w:lvlText w:val="%7."/>
        <w:lvlJc w:val="left"/>
        <w:pPr>
            <w:ind w:left="2940" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="7" w:tplc="04090019" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="lowerLetter"/>
        <w:lvlText w:val="%8)"/>
        <w:lvlJc w:val="left"/>
        <w:pPr>
            <w:ind w:left="3360" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="8" w:tplc="0409001B" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="lowerRoman"/>
        <w:lvlText w:val="%9."/>
        <w:lvlJc w:val="right"/>
        <w:pPr>
            <w:ind w:left="3780" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
</w:abstractNum>
<w:abstractNum w:abstractNumId="1">
    <w:nsid w:val="090656A2"/>
    <w:multiLevelType w:val="hybridMultilevel"/>
    <w:tmpl w:val="B1C8E2E6"/>
    <w:lvl w:ilvl="0" w:tplc="0409000F">
        <w:start w:val="1"/>
        <w:numFmt w:val="decimal"/>
        <w:lvlText w:val="%1."/>
        <w:lvlJc w:val="left"/>
        <w:pPr>
            <w:ind w:left="420" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="1" w:tplc="04090019" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="lowerLetter"/>
        <w:lvlText w:val="%2)"/>
        <w:lvlJc w:val="left"/>
        <w:pPr>
            <w:ind w:left="840" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="2" w:tplc="0409001B" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="lowerRoman"/>
        <w:lvlText w:val="%3."/>
        <w:lvlJc w:val="right"/>
        <w:pPr>
            <w:ind w:left="1260" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="3" w:tplc="0409000F" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="decimal"/>
        <w:lvlText w:val="%4."/>
        <w:lvlJc w:val="left"/>
        <w:pPr>
            <w:ind w:left="1680" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="4" w:tplc="04090019" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="lowerLetter"/>
        <w:lvlText w:val="%5)"/>
        <w:lvlJc w:val="left"/>
        <w:pPr>
            <w:ind w:left="2100" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="5" w:tplc="0409001B" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="lowerRoman"/>
        <w:lvlText w:val="%6."/>
        <w:lvlJc w:val="right"/>
        <w:pPr>
            <w:ind w:left="2520" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="6" w:tplc="0409000F" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="decimal"/>
        <w:lvlText w:val="%7."/>
        <w:lvlJc w:val="left"/>
        <w:pPr>
            <w:ind w:left="2940" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="7" w:tplc="04090019" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="lowerLetter"/>
        <w:lvlText w:val="%8)"/>
        <w:lvlJc w:val="left"/>
        <w:pPr>
            <w:ind w:left="3360" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="8" w:tplc="0409001B" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="lowerRoman"/>
        <w:lvlText w:val="%9."/>
        <w:lvlJc w:val="right"/>
        <w:pPr>
            <w:ind w:left="3780" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
</w:abstractNum>
<w:abstractNum w:abstractNumId="2">
    <w:nsid w:val="4B217E03"/>
    <w:multiLevelType w:val="hybridMultilevel"/>
    <w:tmpl w:val="F87AE51A"/>
    <w:lvl w:ilvl="0" w:tplc="04090013">
        <w:start w:val="1"/>
        <w:numFmt w:val="chineseCountingThousand"/>
        <w:lvlText w:val="%1、"/>
        <w:lvlJc w:val="left"/>
        <w:pPr>
            <w:ind w:left="420" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="1" w:tplc="04090019" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="lowerLetter"/>
        <w:lvlText w:val="%2)"/>
        <w:lvlJc w:val="left"/>
        <w:pPr>
            <w:ind w:left="840" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="2" w:tplc="0409001B" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="lowerRoman"/>
        <w:lvlText w:val="%3."/>
        <w:lvlJc w:val="right"/>
        <w:pPr>
            <w:ind w:left="1260" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="3" w:tplc="0409000F" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="decimal"/>
        <w:lvlText w:val="%4."/>
        <w:lvlJc w:val="left"/>
        <w:pPr>
            <w:ind w:left="1680" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="4" w:tplc="04090019" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="lowerLetter"/>
        <w:lvlText w:val="%5)"/>
        <w:lvlJc w:val="left"/>
        <w:pPr>
            <w:ind w:left="2100" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="5" w:tplc="0409001B" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="lowerRoman"/>
        <w:lvlText w:val="%6."/>
        <w:lvlJc w:val="right"/>
        <w:pPr>
            <w:ind w:left="2520" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="6" w:tplc="0409000F" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="decimal"/>
        <w:lvlText w:val="%7."/>
        <w:lvlJc w:val="left"/>
        <w:pPr>
            <w:ind w:left="2940" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="7" w:tplc="04090019" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="lowerLetter"/>
        <w:lvlText w:val="%8)"/>
        <w:lvlJc w:val="left"/>
        <w:pPr>
            <w:ind w:left="3360" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="8" w:tplc="0409001B" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="lowerRoman"/>
        <w:lvlText w:val="%9."/>
        <w:lvlJc w:val="right"/>
        <w:pPr>
            <w:ind w:left="3780" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
</w:abstractNum>
<w:abstractNum w:abstractNumId="3">
    <w:nsid w:val="6D1A5E1D"/>
    <w:multiLevelType w:val="hybridMultilevel"/>
    <w:tmpl w:val="8D6E3A06"/>
    <w:lvl w:ilvl="0" w:tplc="2EC4773A">
        <w:start w:val="1"/>
        <w:numFmt w:val="japaneseCounting"/>
        <w:lvlText w:val="%1、"/>
        <w:lvlJc w:val="left"/>
        <w:pPr>
            <w:ind w:left="2520" w:hanging="720"/>
        </w:pPr>
        <w:rPr>
            <w:rFonts w:hint="default"/>
        </w:rPr>
    </w:lvl>
    <w:lvl w:ilvl="1" w:tplc="04090019" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="lowerLetter"/>
        <w:lvlText w:val="%2)"/>
        <w:lvlJc w:val="left"/>
        <w:pPr>
            <w:ind w:left="2640" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="2" w:tplc="0409001B" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="lowerRoman"/>
        <w:lvlText w:val="%3."/>
        <w:lvlJc w:val="right"/>
        <w:pPr>
            <w:ind w:left="3060" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="3" w:tplc="0409000F" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="decimal"/>
        <w:lvlText w:val="%4."/>
        <w:lvlJc w:val="left"/>
        <w:pPr>
            <w:ind w:left="3480" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="4" w:tplc="04090019" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="lowerLetter"/>
        <w:lvlText w:val="%5)"/>
        <w:lvlJc w:val="left"/>
        <w:pPr>
            <w:ind w:left="3900" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="5" w:tplc="0409001B" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="lowerRoman"/>
        <w:lvlText w:val="%6."/>
        <w:lvlJc w:val="right"/>
        <w:pPr>
            <w:ind w:left="4320" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="6" w:tplc="0409000F" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="decimal"/>
        <w:lvlText w:val="%7."/>
        <w:lvlJc w:val="left"/>
        <w:pPr>
            <w:ind w:left="4740" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="7" w:tplc="04090019" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="lowerLetter"/>
        <w:lvlText w:val="%8)"/>
        <w:lvlJc w:val="left"/>
        <w:pPr>
            <w:ind w:left="5160" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
    <w:lvl w:ilvl="8" w:tplc="0409001B" w:tentative="true">
        <w:start w:val="1"/>
        <w:numFmt w:val="lowerRoman"/>
        <w:lvlText w:val="%9."/>
        <w:lvlJc w:val="right"/>
        <w:pPr>
            <w:ind w:left="5580" w:hanging="420"/>
        </w:pPr>
    </w:lvl>
</w:abstractNum>
<w:num w:numId="1">
    <w:abstractNumId w:val="3"/>
</w:num>
<w:num w:numId="2">
    <w:abstractNumId w:val="2"/>
</w:num>
<w:num w:numId="3">
    <w:abstractNumId w:val="1"/>
</w:num>
<w:num w:numId="4">
    <w:abstractNumId w:val="0"/>
</w:num>
</w:numbering>

How can I load part from xml file ?


Solution

  • Your error message says something like:

    [org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 5357; 
    and the element type "null" associated with "w: val" 
    attribute values ​​can not contain '< ' character. ]
    

    (Google translated.)

    So I'd guess you simply have an invalid XML where you have an unescaped < in an attribute - or even some XML in that attribute. Make sure your XML is valid.