我有一个带有类似标签的xml文件。

<ENVELOPE>
    <A>Hi
    </A>

    <A1>
        <A12>groupA</A12>
        <A13></A13>
    </A1>
    <B>
        <A>xxx</A>
    </B>
    <C>
        <A11>100</A11>
        <A13></A13>
    </C>
    <B>
        <A>yyy</A>
    </B>
    <C>
        <A11>1000</A11>
        <A13></A13>
    </C>
    <B>
        <A>zzz</A>
    </B>
    <C>
        <A11>100</A11>
        <A13></A13>
    </C>
    <A>Hello</A>
    <A1>
        <A12>groupC</A12>
        <A13></A13>
    </A1>
    <B>
        <A>rrr</A>
    </B>
    <C>
        <A11>10000</A11>
        <A13></A13>
    </C>
</ENVELOPE>


我想以以下格式存储这些数据

Col Header A    A1         B   C-A11
           Hi   groupA    xxx   100
           Hi   groupA    yyy   1000
           Hi   groupA    zzz   100
          Hello groupB    rrr   10000


尝试过的源代码:

   DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
  domFactory.setNamespaceAware(true);
  DocumentBuilder builder = domFactory.newDocumentBuilder();
  Document doc = builder.parse("Response.xml");
  XPath xpath = XPathFactory.newInstance().newXPath();
  // XPath Query for showing all nodes value

  ArrayList names = new ArrayList();

  XPathExpression expr1 = xpath.compile("//A/text()");

  Object res = expr1.evaluate(doc, XPathConstants.NODESET);
  NodeList nod = (NodeList) res;

  for (int i = 0; i < nod.getLength(); i++) {
      System.out.print(i+":");
        System.out.println(nod.item(i).getNodeValue());
        if(!names.contains(nod.item(i).getTextContent().trim()))
            names.add(nod.item(i).getTextContent().trim());
 }

  NodeList nodeLst = doc.getElementsByTagName("C");

   int coun = nodeLst.getLength();
   String val[][] = new String[coun][2];
  for (int s = 0; s < nodeLst.getLength(); s++) {
     Node secNode = nodeLst.item(s);

     if (secNode.getNodeType() == Node.ELEMENT_NODE) {
       try
              {
                Element amtval = (Element) secNode;
                NodeList secval = amtval.getElementsByTagName("A11");
                val[s][0] = secval.item(0).getTextContent();
                    System.out.println("A11 :" + val[s][0]);

                NodeList lstNmElmntLst = amtval.getElementsByTagName("A13");
                val[s][1] = lstNmElmntLst.item(0).getTextContent();
                    System.out.println("A13 : " +val[s][1]);
              }
              catch(Exception ex){
                  ex.printStackTrace();
             }
     }
  }

   NodeList plamt = doc.getElementsByTagName("A1");

   int count = plamt.getLength();
   String val1[][] = new String[count][2];
  for (int s = 0; s < plamt.getLength(); s++) {
      Node secNode = plamt.item(s);

     //if (secNode.getNodeType() == Node.ELEMENT_NODE)
     {
       try
              {
                Element amtval = (Element) secNode;
                NodeList secval = amtval.getElementsByTagName("A12");
                val1[s][0] = secval.item(0).getTextContent();
                    System.out.println("A12 :" + val1[s][0]);

                NodeList lstNmElmntLst = amtval.getElementsByTagName("A13");
                val1[s][1] = lstNmElmntLst.item(0).getTextContent();
                    System.out.println("A13: " +val1[s][1]);
              }
              catch(Exception ex){
                  ex.getMessage();
             }
     }


我低于输出

Hi      100     100
xxx     100         100
yyy     100         100
zzz     100         100
Hello   groupA  groupB
rrr     groupC  groupD



怎么能得到如上所述的格式。
有可用于分组的参考资料吗?
请提供执行此操作的任何逻辑
帮我。
提前致谢,
Dhina P.

最佳答案

我当然不会在导航Java代码中尝试这样做-XSLT解决方案将更加容易。我认为是这样的:

<xsl:for-each select="B">
  <tr>
    <td><xsl:value-of select="preceding-sibling::A[1]"/></td>
    <td><xsl:value-of select="preceding-sibling::A1[1]"/></td>
    <td><xsl:value-of select="child::A"/></td>
    <td><xsl:value-of select="following-sibling::C[1]/A11"></td>
  </tr>
</xsl:for-each>

09-13 01:05