当前位置: 代码迷 >> java >> 如何在XML的特定标签内查找所有标签
  详细解决方案

如何在XML的特定标签内查找所有标签

热度:43   发布时间:2023-08-04 09:31:15.0

我想提取<Page>节点内的所有节点。 我正在使用以下方法通过使用以下两种方法来查找XML文档中的所有节点

doc.getElementsByTagName("*");  //getting all the nodes
doc.getElementsByTagName("name"); //getting nodes <name>

但我想找到特定节点内的所有节点。 例如,我想要<page>内的所有节点。 请给我建议一种方法...

<Pages>
      <Page>
            <Diagram>
                <Widgets>
                    <Image>
                        <Name>YmcLogo</Name>
                        <Rectangle>
                            <Rectangle X="0" Y="4" Width="130" Height="28" />
                        </Rectangle>
                        <Bold>False</Bold>
                        <BorderColor>Color(argb) = (255, 0, 0, 0)</BorderColor>
                        <BorderWidth>-1</BorderWidth>
                        <FillColor>Color(argb) = (255, 255, 255, 255)</FillColor>
                        <FontName>Arial</FontName>
                        <FontSize>9.75</FontSize>
                        <ForeColor>Color(argb) = (255, 0, 0, 0)</ForeColor>
                        <HorizontalAlignment>Center</HorizontalAlignment>
                        <Italic>False</Italic>
                        <Underline>False</Underline>
                        <VerticalAlignment>Center</VerticalAlignment>
                        <Widgets>
                            <TextPanel>
                                <Html>&lt;p style="font-size:13px;text-align:center;line-height:normal;"&gt;&lt;span style="font-family:'Arial Regular', 'Arial';font-weight:400;font-style:normal;font-size:13px;color:#000000;text-align:center;line-height:normal;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;</Html>
                                <Name />
                                <Rectangle>
                                    <Rectangle X="2" Y="6" Width="126" Height="16" />
                                </Rectangle>
                                <Bold>False</Bold>
                                <BorderColor>Color(argb) = (255, 0, 0, 0)</BorderColor>
                                <BorderWidth>-1</BorderWidth>
                                <FillColor>Color(argb) = (255, 255, 255, 255)</FillColor>
                                <FontName>Arial</FontName>
                                <FontSize>9.75</FontSize>
                                <ForeColor>Color(argb) = (255, 0, 0, 0)</ForeColor>
                                <HorizontalAlignment>Center</HorizontalAlignment>
                                <Italic>False</Italic>
                                <Underline>False</Underline>
                                <VerticalAlignment>Center</VerticalAlignment>
                            </TextPanel>
                        </Widgets>
                    </Image>
                        <ShapeType>H2</ShapeType>
                        <Annotation>
                            <Properties>
                                <PropertyValue PropertyName="ContainerType">conditionContainer</PropertyValue>
                            </Properties>
                        </Annotation>
                        <FootnoteNumber>1</FootnoteNumber>
                        <Name>SCMProductGroup</Name>
                        <Rectangle>
                            <Rectangle X="72" Y="110" Width="127" Height="15" />
                        </Rectangle>
                        <Underline>False</Underline>
                        <VerticalAlignment>Near</VerticalAlignment>
                    </Shape>
                    <Textbox>
                        <Text />
                        <Annotation>
                            <Properties>
                                <PropertyValue PropertyName="ContainerType">conditionContainer</PropertyValue>
                                <PropertyValue PropertyName="field_label[多言語対応用キー][多语言对应Key]">label.scmProductGroup</PropertyValue>
                                <PropertyValue PropertyName="type">text</PropertyValue>
                                <PropertyValue PropertyName="cvcodeobjary ">scmProductGrp</PropertyValue>
                                <PropertyValue PropertyName="cvcontainerobjary ">scmProductGrpNm</PropertyValue>
                                <PropertyValue PropertyName="cvfieldstrary ">scmProductGrpName</PropertyValue>
                                <PropertyValue PropertyName="cvopenmethod ">scmProductGrp_ajax_codeValue</PropertyValue>
                                <PropertyValue PropertyName="maxlength[桁数-最大][最大位数]">3</PropertyValue>
                                <PropertyValue PropertyName="size">3</PropertyValue>
                            </Properties>
                        </Annotation>
                    </Textbox>
                    <Textbox>
                        <Text />
                        <Annotation>
                            <Properties>
                                <PropertyValue PropertyName="ContainerType">conditionContainer</PropertyValue>
                                <PropertyValue PropertyName="type">text</PropertyValue>
                                <PropertyValue PropertyName="datatype">String</PropertyValue>
                                <PropertyValue PropertyName="styleClass">display</PropertyValue>
                                <PropertyValue PropertyName="full-width">False</PropertyValue>
                                <PropertyValue PropertyName="half-width-al">True</PropertyValue>
                                <PropertyValue PropertyName="half-width-num">False</PropertyValue>
                                <PropertyValue PropertyName="half-width-other">False</PropertyValue>
                            </Properties>
                        </Annotation>
                    </Textbox>
                    <Table>
                        <Annotation>
                            <Properties>
                                <PropertyValue PropertyName="ContainerType">DhtmlX Grid Container</PropertyValue>
                                <PropertyValue PropertyName="maxlength[桁数-最大][最大位数]">3</PropertyValue>
                                <PropertyValue PropertyName="size">3</PropertyValue>
                                <PropertyValue PropertyName="group-name">1</PropertyValue>
                                <PropertyValue PropertyName="group-type">list</PropertyValue>
                                <PropertyValue PropertyName="collection">result</PropertyValue>
                                <PropertyValue PropertyName="edit[入出力区分][输入区分]">true</PropertyValue>
                                <PropertyValue PropertyName="sort">True</PropertyValue>
                            </Properties>
                        </Annotation>
                        <FootnoteNumber>5</FootnoteNumber>
                        <Name>DHTMLXgrid</Name>
                        <Rectangle>
                            <Rectangle X="20" Y="180" Width="812" Height="140" />
                        </Rectangle>
                        </Table>
                </Widgets>
            </Diagram>
            <PackageInfo>
                <Name>01::inquiry::list</Name>
            </PackageInfo>
      </Page>
</Pages>

获取名称为<page>所有节点

NodeList list = doc.getElementsByTagName("page");

如果有很多,请遍历它们,每获得一个孩子

for (Node node : list)
{
   //Get all nodes inside the this <page> element
   NodeList childList = node.getChildNodes();
}

如果您确实希望每个<page>包含所有节点,则需要递归函数。 这将填充它作为参数获取的列表:

public void getAllChildren(ArrayList<Node> list, Node parentNode)
{
    NodeList childList = parentNode.getChildNodes()
    for(Node node : childList)
    {
       list.add(node);
       getAllChildren(list, node);
    }
}

使用此功能

ArrayList<Node> allNodes = new ArrayList<Node>();

//Get the first node of all elements of <page>
Node pageNode = doc.getElementsByTagName("page").item(0);

getAllChildren(allNodes, pageNode);

//Now every child and child of child etc is on allNodes

获取页面元素,然后使用 (而不是Document.getElementsByTagName )。 例如:

Element pageElement = (Element)doc.getElementsByTagName("Page").item(0);
NodeList result = pageElement.getElementsByTagName("Name");

学习一点 。 XPath是一种小型语言,专门用于获取XML文档的特定部分。

例如,要获取<Page>所有元素,您可以简单地编写//Page/* 或者,如果您想要相同的后代元素,请使用//Page//*

XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("//Page//*");
NodeList result = (NodeList)expr.evaluate(doc, XPathConstants.NODESET);

以供参考 :

  • :我实际上不是用Java编写代码,以上示例代码已从此链接改编而来
  • :基本XPath语法的介绍,如果您有疑问,请与XPath 1.0 进行交叉检查
  相关解决方案