0

Input

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

I have an xml like above, now what I am trying to achieve is if I find this word VIN_Number__c then I want the code to extract only this :

Output

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

and nothing else from the xml.

My code searches for the specific fullName to search for , but how can I extract all the elements from the start tag and end tag of the found element.

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

I am able to identify the keyword but not able to figure out a logic to extract the start tag and end tag contents.

Update

after getting the parent node as suggested by BackSlash, I converted it to string again and got desired output

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();
}
Nagendra Singh
  • 577
  • 1
  • 7
  • 24
  • Why on earth are you reading the entire file into a string? If you want to pass a Reader to your InputSource, why not pass the file-based BufferedReader that you already have? – VGR Sep 27 '18 at 14:16

1 Answers1

0

You can use parentNode:

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

    // other useful code
}
BackSlash
  • 21,927
  • 22
  • 96
  • 136
  • Yes this works, Then I used it to convert to Sting and get the desired result. Updated the nodetoString method in my question. – Nagendra Singh Sep 28 '18 at 01:09