使用java提取XML的所有重复元素

使用java提取XML的所有重复元素,java,xml,xml-parsing,nodes,nodelist,Java,Xml,Xml Parsing,Nodes,Nodelist,我的XML如下所示,我需要在XML中提取多个ID元素 输出xml:- <?xml version="1.0" encoding="utf-8"?> <Stock> <PIdentification> <CatalogVersion></CatalogVersion> <AccountID></AccountID> <CustomerId></CustomerId&

我的XML如下所示,我需要在XML中提取多个ID元素 输出xml:-

<?xml version="1.0" encoding="utf-8"?>
<Stock>
    <PIdentification>
    <CatalogVersion></CatalogVersion>
    <AccountID></AccountID>
    <CustomerId></CustomerId>
    </ProviderIdentification>
    <Product>
        <ArticleName>Monitors</ArticleName>
        <BaseUnit></BaseUnit>
        <Notes></Notes>
        <ID>11f13e2e-ae97-45b5-a9a9-23fa7f6bb767</ID>
        <ID>b22834c0-a570-4e6b-97c3-5067a14d118d</ID>
        <ID>ed458593-5e1a-4dc1-94f0-a66eeef2dd79</ID>
        <ID>d25584a9-1db2-48cf-9a70-9b81e5a7e7f2</ID>
    </Product>
</Stock>
我不能再重复一遍,希望得到所有的身份证,请让我 知道如何获得所有ID,任何帮助都将不胜感激

    private static String toStringXml(Node elt, boolean xmlDeclaration) 
    throws TransformerException {
     TransformerFactory transformerFactory = TransformerFactory
        .newInstance();
    Transformer transformer = transformerFactory.newTransformer();

    if(xmlDeclaration)
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    DOMSource source = new DOMSource(elt);
    StreamResult result = new StreamResult(new StringWriter());
    transformer.transform(source, result);

    return result.getWriter().toString();
    }

您获得了所有id,但您只查看带有
.item(id)
的第一个项目

方法
getElementsByAgename(“ID”)
返回您的
NodeList
,因此您可以通过所有ID获得,例如:

File xmlFile = new File("src/main/resources/example.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document element = dBuilder.parse(xmlFile);

NodeList list = element.getElementsByTagName("ID");
for (int i = 0; i < list.getLength(); i++){
    Node specificIDNode = list.item(i);
    System.out.println(specificIDNode.getTextContent());
}
<?xml version="1.0" encoding="utf-8"?>
<Stock>
    <ProviderIdentification>
        <CatalogVersion></CatalogVersion>
        <AccountID></AccountID>
        <CustomerId></CustomerId>
    </ProviderIdentification>
    <Product>
        <ArticleName>Monitors</ArticleName>
        <BaseUnit></BaseUnit>
        <Notes></Notes>
        <ID>11f13e2e-ae97-45b5-a9a9-23fa7f6bb767</ID>
        <ID>b22834c0-a570-4e6b-97c3-5067a14d118d</ID>
        <ID>ed458593-5e1a-4dc1-94f0-a66eeef2dd79</ID>
        <ID>d25584a9-1db2-48cf-9a70-9b81e5a7e7f2</ID>
    </Product>
</Stock>
File xmlFile=新文件(“src/main/resources/example.xml”);
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
Document元素=dBuilder.parse(xmlFile);
NodeList list=element.getElementsByTagName(“ID”);
对于(int i=0;i
您的问题中显示了格式不正确的XML。第3行上未关闭的
标记

正确的XML代码应如下所示:

File xmlFile = new File("src/main/resources/example.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document element = dBuilder.parse(xmlFile);

NodeList list = element.getElementsByTagName("ID");
for (int i = 0; i < list.getLength(); i++){
    Node specificIDNode = list.item(i);
    System.out.println(specificIDNode.getTextContent());
}
<?xml version="1.0" encoding="utf-8"?>
<Stock>
    <ProviderIdentification>
        <CatalogVersion></CatalogVersion>
        <AccountID></AccountID>
        <CustomerId></CustomerId>
    </ProviderIdentification>
    <Product>
        <ArticleName>Monitors</ArticleName>
        <BaseUnit></BaseUnit>
        <Notes></Notes>
        <ID>11f13e2e-ae97-45b5-a9a9-23fa7f6bb767</ID>
        <ID>b22834c0-a570-4e6b-97c3-5067a14d118d</ID>
        <ID>ed458593-5e1a-4dc1-94f0-a66eeef2dd79</ID>
        <ID>d25584a9-1db2-48cf-9a70-9b81e5a7e7f2</ID>
    </Product>
</Stock>


元素。getElementsByTagName(“Id”)。项(0)
在0处建立索引将返回第一个idtypo:
“Id”
!=<代码>“Id”
您好,我不知道获取所有Id的语法。请告诉我正确的方法。如何解析XML?如果您打印出
list.getLength()
是否只有1?@Sahilkhan测试了它,它确实有效,还将解析部分添加到了答案中。您的
toStringXml
方法可能有问题。嗨,list.getLength()是4,而toStringXml()方法粘贴在Hi@rafael的上方。我又发布了一个问题,我必须从XML中提取一个元素。“需要提取元素进行计算”@Sahilkhan您在这里得到了答案:@Sahilkhan也一样,不要像文本一样处理xml。使用XMLDOM解析器(或StAx解析器)解析XML结构并使用该结构。如果您不了解xml解析器是如何工作的,请使用谷歌搜索。有很多关于它的信息。嗨@Rafael,谢谢!正如我所建议的,我使用了DOM解析器,问题得到了解决。