dom4j new node automatically appear xmlns = ""

Today, the use writing dom4j increase child nodes and sibling nodes elements, encountered a problem: The new child node, comes xmlns = "" attributes, causing the program error.

Baidu long time, finally found a solution, recorded for later reference, reference is also made available to other readers.

When a parent has XMLNS attributes, child node must specify XMLNS property, but when XMLNS property and parent namespace child nodes are the same, the child node is not displayed XMLNS property, the essence is a mistake on our understanding of the above issues, we that there is no specified namespace node as a child, you should not exhibit this property, on the contrary, when we assign the same parent namespace, this property will not occur.
As follows:

 <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">  
          <datasetRun subDataset="Table Dataset 1" uuid="d8b83813-50b2-4ee6-946a-89f74076070a"> 
            <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{userList})]]></dataSourceExpression> 
          </datasetRun>  
          <jr:column width="90" uuid="21ae79ae-dca2-4c43-b531-98b34f2f02bf"> 
            <jr:columnHeader style="table_CH" height="30"> 
              <staticText> 
                <reportElement x="0" y="0" width="90" height="30" uuid="db839944-81d0-4d33-be60-843c1bcfbecc"/>  
                <textElement textAlignment="Center" verticalAlignment="Middle"/>  
                <text><![CDATA[用户id]]></text> 
              </staticText> 
            </jr:columnHeader>  
            <jr:detailCell style="table_TD" height="20"> 
              <textField> 
                <reportElement x="0" y="0" width="90" height="20" uuid="2fbcb727-b009-44fd-bd8e-02c306711fa9"/>  
                <textElement textAlignment="Center" verticalAlignment="Middle"/>  
                <textFieldExpression><![CDATA[$F{uid}]]></textFieldExpression> 
              </textField> 
            </jr:detailCell> 
              </jr:column>     
        </jr:table> 

To add a new jr: column node, to specify the namespace jrcolumn for jr: table of the namespace of its parent node:

        Map map = new HashMap();
        map.put("jr","http://jasperreports.sourceforge.net/jasperreports/components");
	    //创建解析器对象
	    SAXReader saxReader = new SAXReader();
	    File file = new File(filePath);
	    saxReader.getDocumentFactory().setXPathNamespaceURIs(map);
	    //得到document
        Document document = saxReader.read(file);
        Element root = document.getRootElement();
	    //获取到jr:table节点
        List tableList = document.selectNodes("//jr:table");
      
	    Element table = (Element)tableList.get(0);
	    //获取jr:table下面的所有元素
	    List<Element> columns = table.elements();
	    //创建一个新的column节点
	    Element newColumn = DocumentHelper.createElement(QName.get("column", table.getNamespace ()));

关键代码:Element newColumn = DocumentHelper.createElement(QName.get("column", table.getNamespace ()));

The new node specified namespace node is the same as the parent, then the new node with jr: * node on xmlns = "" of the situation, but without xmlns jr node instead of appearing = "no" in the properties like staticText and TextField node, you need to specify the namespace jr without a label, because this is not to xml root node jasperReport jr: at the beginning, so I took root namespace that is:

  Element staticText = columnHeader.addElement(QName.get("staticText", root.getNamespace ()));

Test it, it does not generate xmls: "" property, the problem is solved.

Guess you like

Origin blog.csdn.net/xingqibaing/article/details/91947955