我们可以在XML中带有开始标记和关闭标记的所有元素提取吗?

时间:2018-09-27 12:39:05

标签: java xml xml-parsing

输入

<?xml version="1.0" encoding="UTF-8"?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
    <fields>
        <fullName>VIN_Number1__c</fullName>
        <externalId>false</externalId>
        <label>VIN Number1</label>
        <length>255</length>
        <required>false</required>
        <trackHistory>false</trackHistory>
        <trackTrending>false</trackTrending>
        <type>Text</type>
        <unique>false</unique>
    </fields>
    <fields>
        <fullName>Tub__c</fullName>
        <defaultValue>false</defaultValue>
        <externalId>false</externalId>
        <label>Tub</label>
        <trackHistory>false</trackHistory>
        <trackTrending>false</trackTrending>
        <type>Checkbox</type>
    </fields>
    <fields>
        <fullName>Type__c</fullName>
        <externalId>false</externalId>
        <label>Type</label>
        <required>false</required>
        <trackHistory>false</trackHistory>
        <trackTrending>false</trackTrending>
        <type>Picklist</type>
        <valueSet>
            <valueSetDefinition>
                <sorted>false</sorted>
                <value>
                    <fullName>Luxury</fullName>
                    <default>false</default>
                    <label>Luxury</label>
                </value>
                <value>
                    <fullName>Mid-Range</fullName>
                    <default>false</default>
                    <label>Mid-Range</label>
                </value>
                <value>
                    <fullName>Economy</fullName>
                    <default>false</default>
                    <label>Economy</label>
                </value>
            </valueSetDefinition>
        </valueSet>
    </fields>
    <fields>
        <fullName>VIN_Number__c</fullName>
        <externalId>false</externalId>
        <label>VIN Number</label>
        <length>255</length>
        <required>false</required>
        <trackHistory>false</trackHistory>
        <trackTrending>false</trackTrending>
        <type>Text</type>
        <unique>false</unique>
    </fields>
    <label>Vehicle</label>
    <listViews>
        <fullName>All</fullName>
        <filterScope>Everything</filterScope>
        <label>All</label>
    </listViews>
    <nameField>
        <label>Vehicle Name</label>
        <trackHistory>false</trackHistory>
        <type>Text</type>
    </nameField>
    <pluralLabel>Vehicles</pluralLabel>
    <searchLayouts/>
    <sharingModel>ReadWrite</sharingModel>
    <visibility>Public</visibility>
</CustomObject>

我有一个如上所述的xml,现在我要尝试实现的是,如果我找到这个词VIN_Number__c,那么我希望代码仅提取这个:

输出

   <fields>
        <fullName>VIN_Number__c</fullName>
        <externalId>false</externalId>
        <label>VIN Number</label>
        <length>255</length>
        <required>false</required>
        <trackHistory>false</trackHistory>
        <trackTrending>false</trackTrending>
        <type>Text</type>
        <unique>false</unique>
    </fields>

以及xml中的其他内容。

我的代码搜索要搜索的特定fullName,但是如何从找到的元素的开始标签和结束标签中提取所有元素。

    String[] strings = member.split("\\.");
File dir = new File("C:\\Users\\nagesingh\\IdeaProjects\\deploymentMaster\\src\\main\\java\\objects");
File[] directoryListing = dir.listFiles();
if (directoryListing != null) {
    for (File child : directoryListing) {
        String name = child.getName();
        String[] fileNameSplit = name.split("\\.");
        if(strings[0].equalsIgnoreCase(fileNameSplit[0])){
            FileReader fileReader = new FileReader(child.getAbsoluteFile());
            BufferedReader br = new BufferedReader(fileReader);
            String line;
            StringBuilder sb = new StringBuilder();

            while((line=br.readLine())!= null){
                sb.append(line.trim());
            }
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            InputSource src = new InputSource();
            src.setCharacterStream(new StringReader(sb.toString()));

            Document doc = builder.parse(src);
                NodeList fullName = doc.getElementsByTagName("fullName");
                if(fullName.getLength() > 0){
                    for (int i = 0; i < fullName.getLength(); i++) {
                        String nameExtracted = doc.getElementsByTagName("fullName").item(i).getTextContent();
                        if(nameExtracted.equalsIgnoreCase(strings[1])){
                           // here it identifies the  'VIN_Number__c'
                        }
                    }
                }      
        }
    }
}

我能够识别关键字,但无法弄清楚提取开始标签和结束标签内容的逻辑。

更新

按照BackSlash的建议获取父节点后,我再次将其转换为字符串并获得了所需的输出

private String nodeToString(Node node) {
    StringWriter sw = new StringWriter();
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.transform(new DOMSource(node), new StreamResult(sw));
    } catch (TransformerException te) {
        System.out.println("nodeToString Transformer Exception");
    }
    return sw.toString();
}

1 个答案:

答案 0 :(得分:0)

您可以使用parentNode

if(nameExtracted.equalsIgnoreCase(strings[1])){
    Node fieldsNode = fullName.item(i).getParentNode();

    // other useful code
}