-5

i want to split my below string to get the expected output

<?xml version="1.0" encoding="UTF-8" ?>
<CompareInformation>
    <request>
        <OrderInformation>
            <CardTypeUsed>CreditCard</CardTypeUsed>
            <consumerId>GIF</consumerId>
        </OrderInformation>
    </request>
</CompareInformation>

Expected output

<OrderInformation>
            <CardTypeUsed>CreditCard</CardTypeUsed>
            <consumerId>GIF</consumerId>
        </OrderInformation>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Suryaneel Varshney
  • 85
  • 1
  • 2
  • 13
  • 1
    What's the criteria to split? Seems like it's not split but substring and looks like you want to extract a XML element. – supernova Apr 22 '21 at 08:46
  • i want to remove till request from top and below everything request from down – Suryaneel Varshney Apr 22 '21 at 09:28
  • Does this answer your question? [How to read XML using XPath in Java](https://stackoverflow.com/questions/2811001/how-to-read-xml-using-xpath-in-java) – Alden W. Apr 22 '21 at 20:52

2 Answers2

1

I believe that dealing with XML suits better a XML library. For changing XML you can use XSLT, which has the benefit that it can also be used in CLI and other tools and languages without any change. XSLT is exactly build for what you do - transform XML.

If you are sure you will ever stay in your app you can also use a common Java XML lib of course.

Here's an example for your use case:

package org.example;

import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URISyntaxException;

public class XSLTDemo {


    public static void main(String[] args) throws IOException, URISyntaxException, TransformerException {
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
                "<CompareInformation>" +
                "    <request>" +
                "        <OrderInformation>" +
                "            <CardTypeUsed>CreditCard</CardTypeUsed>"+
                "            <consumerId>GIF</consumerId>"+
                "        </OrderInformation>"+
                "    </request>"+
                "</CompareInformation>";

        String xsltStr = "<?xml version=\"1.0\"?>\n" +
                "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n"+
                "<xsl:output method=\"xml\" omit-xml-declaration=\"yes\" />\n" +
                "    <xsl:template match=\"/\">\n" +
                "            <xsl:copy-of select=\"/CompareInformation/node()\" />\n" +
                "    </xsl:template>\n" +
                "</xsl:stylesheet>";

        TransformerFactory factory = TransformerFactory.newInstance();
        Source xslt = new StreamSource(new StringReader(xsltStr));
        Transformer transformer = factory.newTransformer(xslt);

        Source text = new StreamSource(new StringReader(xml));
        StringWriter writer = new StringWriter();
        transformer.transform(text, new StreamResult(writer));

        System.out.println(writer.toString());
    }
}
supernova
  • 1,762
  • 1
  • 14
  • 31
0

For something like this you really should use a proper XML parsing library, but if you have to do it like this, then simply using a split here would be unnecessarily complex as splitting is meant for, well, literally splitting a string by a delimiter which wouldn't work because you have two delimiters here (<OrderInformation> and </OrderInformation>).

You'd want to do something like this:

public static String extract(String input, String start, String end) {
    int length = end.length();
    int startIndex = input.indexOf(start);
    // because indexOf returns index of the first character found, 
    // we need to add the length of the "end" string to get the index of the last character.
    int endIndex = input.indexOf(end) + length;

    return input.substring(startIndex, endIndex);
}
String result = extract(in, "<OrderInformation>", "</OrderInformation>")

You can read more about substring() here.

Keep in mind that this solution won't work if there's multiple occurrences of <OrderInformation>. You would need to search for multiple occurrences manually and return some sort of an array.

SocketByte
  • 335
  • 4
  • 12