0

我有一个带有标签的 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
  1. 怎样才能得到上述格式。
  2. 有没有可用于分组的参考资料?
  3. 请提供任何逻辑来帮助我。在此先感谢, 中国 P.
4

1 回答 1

2

我当然不会在导航 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>
于 2012-05-14T16:04:58.513 回答