本文介绍了我想提取所有< link>使用Java从此XML树中添加标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML文件,该文件具有3级深度的XML树.我想从那棵树中提取所有< link> 标记.这里的挑战是树是动态的,并且可以是任何层次的深度.

I have an XML file which has a 3 level deep XML tree. I want to extract all the <link> tags from that tree. The challenge here is that tree is dynamic and can be any level deep.

我已经提取了根节点< helplinks> ,并且我有两种方法可供使用:

I have already extracted the root node <helplinks> and I have two methods at my disposal:

->XmlUtil.getChildNodes(节点节点,子节点的名称")~~从父节点返回子节点的列表.

-> XmlUtil.getChildNodes(Node node,"name of the child") ~~ returns a list of child nodes from the parent node.

<helplinks message="Check these links out if you are trying to accomplish something like this: \\n">
        <category name="Connectivity" value = "1" jsonValue="MEP">
            <link name="Reset Password" value="https://www.google.com"></link>
            <link name="Rename User" value="https://www.google.com"></link>
            <link name="Change Database" value="https://www.google.com"></link>
            <subcategory name="Internet" value = "1" jsonValue="MES">
                <link name="Option 1" value="https://www.google.com"></link>
                <link name="Option 2" value="https://www.google.com"></link>
                <item name="Not Working" value = "1" jsonValue="MDS">
                    <link name="Option 1" value="https://www.google.com"></link>
                    <link name="Option 2" value="https://www.google.com"></link>
                    <link name="Option 3" value="https://www.google.com"></link>
                    <link name="Option 4" value="https://www.google.com"></link>
                </item>
                <item name="Working Slow" value = "2" jsonValue="MLP">
                    <link name="Option 1" value="https://www.google.com"></link>
                    <link name="Option 2" value="https://www.google.com"></link>
                    <link name="Option 3" value="https://www.google.com"></link>
                    <link name="Option 4" value="https://www.google.com"></link>
                    <link name="Option 5" value="https://www.google.com"></link>
                </item>
            </subcategory>
            <subcategory name="VPN" value = "2" jsonValue="MES">
                <link name="Option 1" value="https://www.google.com"></link>
                <link name="Option 2" value="https://www.google.com"></link>
                <link name="Option 3" value="https://www.google.com"></link>
                <link name="Option 4" value="https://www.google.com"></link>
                <link name="Option 5" value="https://www.google.com"></link>
                <item name="Not Working" value = "1" jsonValue="MDS">
                    <link name="Option 1" value="https://www.google.com"></link>
                    <link name="Option 2" value="https://www.google.com"></link>
                </item>
                <item name="Working Slow" value = "2" jsonValue="MLP">
                    <link name="Option 1" value="https://www.google.com"></link>
                    <link name="Option 2" value="https://www.google.com"></link>
                    <link name="Option 3" value="https://www.google.com"></link>
                </item>
            </subcategory>
        </category>
        <category name="Desktop/Laptop Issue" value = "2" jsonValue="LPM">
            <link name="Option 1" value="https://www.google.com"></link>
            <link name="Option 2" value="https://www.google.com"></link>
            <subcategory name="Machine Down" value = "1" jsonValue="LPN">
                <link name="Option 1" value="https://www.google.com"></link>
                <link name="Option 2" value="https://www.google.com"></link>
                <link name="Option 3" value="https://www.google.com"></link>
                <item name="Power Down" value = "1" jsonValue="LMR">
                    <link name="Option 1" value="https://www.google.com"></link>
                    <link name="Option 2" value="https://www.google.com"></link>
                    <link name="Option 3" value="https://www.google.com"></link>
                    <link name="Option 4" value="https://www.google.com"></link>
                </item>
                <item name="Blank Screen" value = "2" jsonValue="LMV">
                    <link name="Option 1" value="https://www.google.com"></link>
                    <link name="Option 2" value="https://www.google.com"></link>
                </item>
            </subcategory>
            <subcategory name="Machine Slow" value = "2" jsonValue="LPO">
                <link name="Option 1" value="https://www.google.com"></link>
                <link name="Option 2" value="https://www.google.com"></link>
                <link name="Option 3" value="https://www.google.com"></link>
                <item name="Laptop" value = "1" jsonValue="LMA">
                    <link name="Option 1" value="https://www.google.com"></link>
                    <link name="Option 2" value="https://www.google.com"></link>
                </item>
                <item name="Desktop" value = "2" jsonValue="LMQ">
                    <link name="Option 1" value="https://www.google.com"></link>
                </item>
            </subcategory>
            <subcategory name="Need Upgrade" value = "3" jsonValue="LPC">
                <link name="Option 1" value="https://www.google.com"></link>
                <link name="Option 2" value="https://www.google.com"></link>
                <link name="Option 3" value="https://www.google.com"></link>
                <item name="Laptop" value = "1" jsonValue="LMT">
                    <link name="Option 1" value="https://www.google.com"></link>
                    <link name="Option 2" value="https://www.google.com"></link>
                    <link name="Option 3" value="https://www.google.com"></link>
                </item>
                <item name="Desktop" value = "2" jsonValue="LMP">
                    <link name="Option 1" value="https://www.google.com"></link>
                    <link name="Option 2" value="https://www.google.com"></link>
                    <link name="Option 3" value="https://www.google.com"></link>
                </item>
            </subcategory>
            <subcategory name="Hardware Problem" value = "4" jsonValue="LPR">
                <link name="Option 1" value="https://www.google.com"></link>
                <link name="Option 2" value="https://www.google.com"></link>
                <link name="Option 3" value="https://www.google.com"></link>
                <link name="Option 4" value="https://www.google.com"></link>
                <link name="Option 5" value="https://www.google.com"></link>
                <link name="Option 6" value="https://www.google.com"></link>
                <item name="Screen Problem" value = "1" jsonValue="LMW">
                    <link name="Option 1" value="https://www.google.com"></link>
                    <link name="Option 2" value="https://www.google.com"></link>
                    <link name="Option 3" value="https://www.google.com"></link>
                </item>
                <item name="HardDisk Problem" value = "2" jsonValue="LMG">
                    <link name="Option 1" value="https://www.google.com"></link>
                    <link name="Option 2" value="https://www.google.com"></link>
                    <link name="Option 3" value="https://www.google.com"></link>
                </item>
                <item name="Keyboard Problem" value = "3" jsonValue="LMH">
                    <link name="Option 1" value="https://www.google.com"></link>
                    <link name="Option 2" value="https://www.google.com"></link>
                    <link name="Option 3" value="https://www.google.com"></link>
                </item>
                <item name="Others" value = "4" jsonValue="LMI">
                    <link name="Option 1" value="https://www.google.com"></link>
                    <link name="Option 2" value="https://www.google.com"></link>
                    <link name="Option 3" value="https://www.google.com"></link>
                </item>
            </subcategory>
        </category>
        <category name="Business Application Issue" value = "3" jsonValue="ABC">
            <link name="Option 1" value="https://www.google.com"></link>
            <link name="Option 2" value="https://www.google.com"></link>
            <link name="Option 3" value="https://www.google.com"></link>
            <subcategory name="ERP" value = "1" jsonValue="ABC1">
                <link name="Option 1" value="https://www.google.com"></link>
                <link name="Option 2" value="https://www.google.com"></link>
                <link name="Option 3" value="https://www.google.com"></link>
                <link name="Option 4" value="https://www.google.com"></link>
                <link name="Option 5" value="https://www.google.com"></link>
                <link name="Option 6" value="https://www.google.com"></link>
                <item name="Login Issue" value = "1" jsonValue="ABC11">
                    <link name="Option 1" value="https://www.google.com"></link>
                    <link name="Option 2" value="https://www.google.com"></link>
                    <link name="Option 3" value="https://www.google.com"></link>
                    <link name="Option 4" value="https://www.google.com"></link>
                    <link name="Option 5" value="https://www.google.com"></link>
                    <link name="Option 6" value="https://www.google.com"></link>
                </item>
                <item name="Workflow Issue" value = "2" jsonValue="ABC12">
                    <link name="Option 1" value="https://www.google.com"></link>
                    <link name="Option 2" value="https://www.google.com"></link>
                    <link name="Option 3" value="https://www.google.com"></link>
                </item>
            </subcategory>
            <subcategory name="CRM" value = "2" jsonValue="ABC2">
                <link name="Option 1" value="https://www.google.com"></link>
                <link name="Option 2" value="https://www.google.com"></link>
                <link name="Option 3" value="https://www.google.com"></link>
                <item name="Login Issue" value = "1" jsonValue="ABC21">
                    <link name="Option 1" value="https://www.google.com"></link>
                    <link name="Option 2" value="https://www.google.com"></link>
                    <link name="Option 3" value="https://www.google.com"></link>
                    <link name="Option 4" value="https://www.google.com"></link>
                    <link name="Option 5" value="https://www.google.com"></link>
                    <link name="Option 6" value="https://www.google.com"></link>
                </item>
                <item name="Workflow Issue" value = "2" jsonValue="ABC22">
                    <link name="Option 1" value="https://www.google.com"></link>
                    <link name="Option 2" value="https://www.google.com"></link>
                    <link name="Option 3" value="https://www.google.com"></link>
                    <link name="Option 4" value="https://www.google.com"></link>
                    <link name="Option 5" value="https://www.google.com"></link>
                    <link name="Option 6" value="https://www.google.com"></link>
                </item>
            </subcategory>
            <subcategory name="Email" value = "3" jsonValue="ABC3">
                <link name="Option 1" value="https://www.google.com"></link>
                <link name="Option 2" value="https://www.google.com"></link>
                <link name="Option 3" value="https://www.google.com"></link>
                <item name="Send Receive Issue" value = "1" jsonValue="ABC31">
                    <link name="Option 1" value="https://www.google.com"></link>
                    <link name="Option 2" value="https://www.google.com"></link>
                    <link name="Option 3" value="https://www.google.com"></link>
                    <link name="Option 4" value="https://www.google.com"></link>
                    <link name="Option 5" value="https://www.google.com"></link>
                    <link name="Option 6" value="https://www.google.com"></link>
                </item>
                <item name="Archive Issue" value = "2" jsonValue="ABC32">
                    <link name="Option 1" value="https://www.google.com"></link>
                    <link name="Option 2" value="https://www.google.com"></link>
                    <link name="Option 3" value="https://www.google.com"></link>
                </item>
                <item name="Space issue" value = "3" jsonValue="ABC33">
                    <link name="Option 1" value="https://www.google.com"></link>
                    <link name="Option 2" value="https://www.google.com"></link>
                    <link name="Option 3" value="https://www.google.com"></link>
                </item>
            </subcategory>
        </category>
    </helplinks>

要明确地说,我的目标是提取所有< link> 标签,而不管嵌套如何.

To be clearly stated my goal is to extract out all the <link> tags regardless of the nesting.

推荐答案

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import java.io.File;
import java.util.Map;

public class Test {
    public static void main(String[] args) throws Exception {
        File file = new File("C:\\Users\\rishabh.awatani\\Downloads\\something.xml");
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(file);
        XPath xPath = XPathFactory.newInstance().newXPath();
        String expression = "//link";
        NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
        for(int i = 0; i<nodeList.getLength(); i++){
            Node node = nodeList.item(i);
            NamedNodeMap nodeMap = node.getAttributes();
            System.out.println(nodeMap.getNamedItem("name").getNodeValue() + " ----> " + nodeMap.getNamedItem("value").getNodeValue());
        }
    }
}

这对我有用,因为我不需要担心树结构.使用XPath查找所有< link> 标记,然后提取出不同元素的值.

This worked for me as Tree structure was none of my concern. Using XPath to find all the <link> tags and then extracting out values of different elements.

这篇关于我想提取所有&lt; link&gt;使用Java从此XML树中添加标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:57