2017-06-02 38 views
1

子节点,我需要帮助使用dom4j的解析器在一个的docx XML文件访问子节点。试图访问的docx文件

我创建使用节点列表:

List<Node> nodes = document.selectNodes("/w:document/w:body/w:tbl/w:tr/w:tc"); 

但是,林不知道如何找到selectNode组的子节点。 docx文件是一个列表,我编辑并试图更新我们的数据库。

我需要保持在这个水平上,因为我需要知道数据是哪一列。 我需要去的数量

我想在水平getchild。我需要确定是否有任何数据在任何列中丢失。

感谢你的帮助 <

w:tc> 
    <w:tcPr> 
     <w:shd w:val="clear" w:color="auto" w:fill="FFFFFF"/> 
     <w:tcBorders> 
      <w:left w:val="single" w:sz="4"/> 
      <w:top w:val="single" w:sz="4"/> 
     </w:tcBorders> 
     <w:vAlign w:val="top"/> 
    </w:tcPr> 
    <w:p> 
     <w:pPr> 
      <w:pStyle w:val="Style2"/> 
      <w:framePr w:w="10805" w:wrap="notBeside" w:vAnchor="text" w:hAnchor="text" w:xAlign="center" w:y="1"/> 
      <w:widowControl w:val="0"/> 
      <w:keepNext w:val="0"/> 
      <w:keepLines w:val="0"/> 
      <w:shd w:val="clear" w:color="auto" w:fill="auto"/> 
      <w:bidi w:val="0"/> 
      <w:jc w:val="left"/> 
      <w:spacing w:before="0" w:after="0" w:line="190" w:lineRule="exact"/> 
      <w:ind w:left="200" w:right="0" w:firstLine="0"/> 
     </w:pPr> 
     <w:r> 
      <w:rPr> 
       <w:rStyle w:val="CharStyle15"/> 
      </w:rPr> 
      <w:t>Quantity</w:t> 
     </w:r> 
    </w:p> 
</w:tc> 
+0

可不可以给你解析XML的片段?您必须选择哪些子节点? – tnas

回答

0

要查找当前上下文节点的子节点,你必须指定RelativeLocationPath其不受'/'字符开始。

for (Iterator iterator = nodes.iterator(); iterator.hasNext();) { 
    Node node = (Node) iterator.next(); 
    node.selectNodes("w:tcPr");//="child::w:tcPr", child is the default axis of location path 
    //The API return child nodes `w:tcPr` 
} 

node.selectNodes( “W:TCPR/W:SHD”);

API返回1节点[/ w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:shd];

node.selectNodes(“*/w:shd”); //选择上下文节点的大孩子“w:shd”

API return 1 node [w:tcPr/w:shd];如果节点w:p/w:shd存在,它将被选中。

node.selectNodes(“self :: node()// w:shd”); //的XPath = “自::节点()/后代或自身::节点()/ W:SHD”

API返回2个节点[w:tcPr/w:shdw:p/w:pPr/w:shd];如果节点w:shd存在,它将被选中。

node.selectNodes( “* //瓦特:SHD”); //的XPath = “子::节点()/后代或自身::节点()/ W:SHD”

API还返回2个节点[w:tcPr/w:shdw:p/w:pPr/w:shd];但是如果节点w:shd存在,将会选择而不是

+0

如何选择答案? – TYD

+0

你是什么意思?如果你想获得文本节点“数量”,你可以调用'node.selectSingleNode(“w:p/w:r/w:t”)。getText()'。 –