获取标签和值?XML(复制)

时间:2021-11-15 21:44:01

This question already has an answer here:

这个问题已经有了答案:

I have the following XML being passed as String.

我有以下XML作为字符串传递。

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

我调用下面的方法,它为每个标签获取me值。

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

4 个解决方案

#1


2  

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.

使用LSSerializer (http://docs.oracle.com/javase/8/docs/api/index.html?

#2


1  

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


1  

It is a very simple piece of code with XPath and VTD-XML

这是一段使用XPath和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


1  

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

因为,每次调用该方法时,XML都会有相同的标签名称,即标签1-4。所以,我把这种方法....

    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:

下面是拆分tag1, tag2, tag3的方法:

    String tag1 = split(XMLString, "<tag1>", "<tag2>"));
    String tag2 = split(XMLString, "<tag2>", "<tag3>"));
    String tag3 = split(XMLString, "<tag3>", "<tag4>"));

#1


2  

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.

使用LSSerializer (http://docs.oracle.com/javase/8/docs/api/index.html?

#2


1  

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


1  

It is a very simple piece of code with XPath and VTD-XML

这是一段使用XPath和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


1  

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

因为,每次调用该方法时,XML都会有相同的标签名称,即标签1-4。所以,我把这种方法....

    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:

下面是拆分tag1, tag2, tag3的方法:

    String tag1 = split(XMLString, "<tag1>", "<tag2>"));
    String tag2 = split(XMLString, "<tag2>", "<tag3>"));
    String tag3 = split(XMLString, "<tag3>", "<tag4>"));