问题
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>";
回答1:
Either use LSSerializer
(http://docs.oracle.com/javase/8/docs/api/index.html?org/w3c/dom/ls/LSSerializer.html) or create a default Transformer
from a TransformerFactory
and then you can use that to serialize a DOM node, passing in a DOMSource
to the transform
method (https://docs.oracle.com/javase/7/docs/api/javax/xml/transform/Transformer.html#transform(javax.xml.transform.Source,%20javax.xml.transform.Result) and a StringWriter to collect the result.
回答2:
I am not sure if this functionality already exists, but you could write a simple helper method:
private static String getWrappedTag(String tag, Document doc) {
StringBuilder sb = new StringBuilder();
sb.append("<" + tag + ">");
sb.append(doc.getElementsByTagName(tag).item(0).getTextContent());
sb.append("</" + tag + ">");
return sb.toString();
}
And call it like this:
String tag1 = getWrappedTag("tag1", doc);
回答3:
It is a very simple piece of code with XPath and VTD-XML
import com.ximpleware.*;
import java.io.*;
public class splitXML {
public static void main(String[] args) throws VTDException, IOException {
VTDGen vg = new VTDGen();
if (!vg.parseFile("d:\\xml\\input.xml", false)){
System.out.println("error");
return;
}
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("/tagmain/*");
int i=0,n=0;
FileOutputStream fos =null;
while((i=ap.evalXPath())!=-1){
fos = new FileOutputStream("d:\\xml\\output"+(++n)+".xml");
long l = vn.getElementFragment();
fos.write(vn.getXML().getBytes(), (int)l, (int)(l>>32));
fos.close();
}
}
}
回答4:
Thanks guys for your input.
Since, every time the method was being called the XML was going to have the same tag names meaning tags 1-4. so, I took this approach....
public String split(String xml, String tagName1, String tagName2)
{
String splitedXML = xml.substring((xml.indexOf(tagName1)), xml.indexOf(tagName2));
return splitedXML;
}
Now this is how I would call the method to split tag1, tag2, tag3 values:
String tag1 = split(XMLString, "<tag1>", "<tag2>"));
String tag2 = split(XMLString, "<tag2>", "<tag3>"));
String tag3 = split(XMLString, "<tag3>", "<tag4>"));
来源:https://stackoverflow.com/questions/39151102/getting-tag-as-well-as-the-values-xml