I have the following XML being passed as String.
<?xml version="1.0"?>
<tagMain>
<tag1>
<a>
<a>1</a>
<b>2</b>
<c>3</c>
<d>4</d>
</a>
<b>5</b>
<c>6</c>
<d>7</d>
<e>8</e>
<f>9</f>
</tag1>
<tag2>
<r>
<r1>10</r1>
<r2>11</r2>
<r3>12</r3>
<r4>13</r4>
</r>
<b>14</b>
<c>15</c>
<d>16</d>
<e>17</e>
<f>18</f>
</tag2>
<tag3>
<a>
<a>1m</a>
<b>2m</b>
<c>3m</c>
<d>4m</d>
</a>
<b>5m</b>
<c>6m</c>
<d>7m</d>
<e>8m</e>
<f>9m</f>
</tag3>
</tagMain>
I calling the following method which is getting me values between for each tag.
public static void SplitXml(String xml) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilder builder = DocumentBuilderFactory
.newInstance().newDocumentBuilder();
InputSource src = new InputSource();
src.setCharacterStream(new StringReader(xml));
Document docu = builder.parse(src);
String tag1 = docu.getElementsByTagName("tag1").item(0).getTextContent();
String tag2 = docu.getElementsByTagName("tag2").item(0).getTextContent();
String tag3 = docu.getElementsByTagName("tag3").item(0).getTextContent();
}
when I run the above code:
tag1 = "123456789";
tag2 = "101112131415161718";
tag3 = "1m2m3m4m5m6m7m8m9m";
Now my back to my question, Is there a way I can get the tags as well as the values inside the like such for each tag:
tag1 = "<tag1><a>
<a>1</a>
<b>2</b>
<c>3</c>
<d>4</d>
</a>
<b>5</b>
<c>6</c>
<d>7</d>
<e>8</e>
<f>9</f>
</tag1>";