Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在XML中查找特定标记内的所有标记_Java_Xml - Fatal编程技术网

Java 如何在XML中查找特定标记内的所有标记

Java 如何在XML中查找特定标记内的所有标记,java,xml,Java,Xml,我想提取节点中的所有节点。 我使用以下两种方法查找XML文档中的所有节点 doc.getElementsByTagName("*"); //getting all the nodes doc.getElementsByTagName("name"); //getting nodes <name> doc.getElementsByTagName(“*”)//获取所有节点 文件getElementsByTagName(“名称”)//获取节点 但是我想找到特定节点中的所有节点。例如

我想提取
节点中的所有节点。 我使用以下两种方法查找XML文档中的所有节点

doc.getElementsByTagName("*");  //getting all the nodes
doc.getElementsByTagName("name"); //getting nodes <name>
doc.getElementsByTagName(“*”)//获取所有节点
文件getElementsByTagName(“名称”)//获取节点
但是我想找到特定节点中的所有节点。例如,我想要
中的所有节点。请给我建议一个方法

<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>

伊姆克罗戈
假的
颜色(argb)=(255,0,0,0)
-1
颜色(argb)=(255、255、255、255)
Arial
9.75
颜色(argb)=(255,0,0,0)
居中
假的
假的
居中
p style=“font-size:13px;文本对齐:中心;线条高度:正常;”span style=“font-family:'Arial Regular','Arial';字体重量:400;字体样式:正常;字体大小:13px;颜色:#000000;文本对齐:中心;线条高度:正常;”&;nbsp/span/p
假的
颜色(argb)=(255,0,0,0)
-1
颜色(argb)=(255、255、255、255)
Arial
9.75
颜色(argb)=(255,0,0,0)
居中
假的
假的
居中
氢
条件容器
1.
SCMProductGroup
假的
近的
条件容器
label.scmProductGroup
文本
scmProductGrp
scmProductGrpNm
scmProductGrpName
scmProductGrp\u ajax\u code值
3.
3.
条件容器
文本
一串
显示
假的
真的
假的
假的
DhtmlX网格容器
3.
3.
1.
列表
结果
真的
真的
5.
DHTMLXgrid
01::查询::列表

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

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

获取名称为

如果有很多,迭代它们并为每个获取子级

for (Node node : list)
{
   //Get all nodes inside the this <page> element
   NodeList childList = node.getChildNodes();
}
for(节点:列表)
{
//获取此元素内的所有节点
NodeList childList=node.getChildNodes();
}
如果您确实希望每个
中包含所有节点,则需要一个递归函数。此参数将填充作为参数获取的列表:

public void getAllChildren(ArrayList<Node> list, Node parentNode)
{
    NodeList childList = parentNode.getChildNodes()
    for(Node node : childList)
    {
       list.add(node);
       getAllChildren(list, node);
    }
}
public void getAllChildren(ArrayList列表,节点parentNode)
{
NodeList childList=parentNode.getChildNodes()
用于(节点:子列表)
{
列表。添加(节点);
getAllChildren(列表,节点);
}
}
要使用此功能

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
ArrayList allNodes=new ArrayList();
//获取的所有元素的第一个节点
节点pageNode=doc.getElementsByTagName(“页面”)。项(0);
getAllChildren(allNodes,pageNode);
//现在,每个子节点和子节点etc的子节点都位于所有节点上
学习一点。XPath是一种专门为获取XML文档的特定部分而设计的小型语言

例如,要获取
的所有子元素,只需编写
//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的
结果将仅包含节点列表。。。但是我想要一个特定节点中的所有节点。例如document.getElementsByTagName(“*”)=>这将带来所有
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("//Page//*");
NodeList result = (NodeList)expr.evaluate(doc, XPathConstants.NODESET);