2016-09-27 2 views
0

XML을 CSV 데이터로 변환하려고했습니다. 다양한 예제를 통해 XML 파일을 구문 분석하고 CSV 파일을 가져 오는 코드를 작성할 수있었습니다. 그러나 필자가 작성한 코드는 XML 파일에있는 모든 태그를 표시하지 않는 CSV 파일을 반환합니다.XML to CSV 변환 Java

변환을위한 XSLT가 있습니다. 나는 XSLT에 익숙하지 않아서 내 XSLT에 문제가 있다고 생각한다.

<?xml version="1.0"?> 

<school id="100" name="WGen School"> 

    <grade id="1"> 
     <classroom id="101" name="Mrs. Jones' Math Class"> 
      <teacher id="10100000001" first_name="Barbara" last_name="Jones"/> 

      <student id="10100000010" first_name="Michael" last_name="Gil"/> 
      <student id="10100000011" first_name="Kimberly" last_name="Gutierrez"/> 
      <student id="10100000013" first_name="Toby" last_name="Mercado"/> 
      <student id="10100000014" first_name="Lizzie" last_name="Garcia"/> 
      <student id="10100000015" first_name="Alex" last_name="Cruz"/> 
     </classroom> 


     <classroom id="102" name="Mr. Smith's PhysEd Class"> 
      <teacher id="10200000001" first_name="Arthur" last_name="Smith"/> 
      <teacher id="10200000011" first_name="John" last_name="Patterson"/> 

      <student id="10200000010" first_name="Nathaniel" last_name="Smith"/> 
      <student id="10200000011" first_name="Brandon" last_name="McCrancy"/> 
      <student id="10200000012" first_name="Elizabeth" last_name="Marco"/> 
      <student id="10200000013" first_name="Erica" last_name="Lanni"/> 
      <student id="10200000014" first_name="Michael" last_name="Flores"/> 
      <student id="10200000015" first_name="Jasmin" last_name="Hill"/> 
      <student id="10200000016" first_name="Brittany" last_name="Perez"/> 
      <student id="10200000017" first_name="William" last_name="Hiram"/> 
      <student id="10200000018" first_name="Alexis" last_name="Reginald"/> 
      <student id="10200000019" first_name="Matthew" last_name="Gayle"/> 
     </classroom> 

     <classroom id="103" name="Brian's Homeroom"> 
      <teacher id="10300000001" first_name="Brian" last_name="O'Donnell"/> 
     </classroom> 
    </grade> 
</school> 
:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text"/> 

    <xsl:template match="/"> 
    <xsl:for-each select="*/*[1]/*"> 
     <xsl:value-of select="name()" /> 
     <xsl:if test="not(position() = last())">,</xsl:if> 
    </xsl:for-each> 
    <xsl:text>&#10;</xsl:text> 
    <xsl:apply-templates select="*/*" mode="row"/> 
    </xsl:template> 

    <xsl:template match="*" mode="row"> 
    <xsl:apply-templates select="*" mode="data" /> 
    <xsl:text>&#10;</xsl:text> 
    </xsl:template> 

    <xsl:template match="*" mode="data"> 
    <xsl:choose> 
     <xsl:when test="contains(text(),',')"> 
     <xsl:text>&quot;</xsl:text> 
     <xsl:call-template name="doublequotes"> 
      <xsl:with-param name="text" select="text()" /> 
     </xsl:call-template> 
     <xsl:text>&quot;</xsl:text> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:value-of select="." /> 
     </xsl:otherwise> 
    </xsl:choose> 
    <xsl:if test="position() != last()">,</xsl:if> 
    </xsl:template> 

    <xsl:template name="doublequotes"> 
    <xsl:param name="text" /> 
    <xsl:choose> 
     <xsl:when test="contains($text,'&quot;')"> 
     <xsl:value-of select="concat(substring-before($text,'&quot;'),'&quot;&quot;')" /> 
     <xsl:call-template name="doublequotes"> 
      <xsl:with-param name="text" select="substring-after($text,'&quot;')" /> 
     </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:value-of select="$text" /> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:template> 
</xsl:stylesheet> 

이 내가 CSV로 변환하기 위해 노력하고있어 XML 파일은 다음과 같습니다

package com.adarsh.conversions; 

import java.io.File; 
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.transform.Result; 
import javax.xml.transform.Source; 
import javax.xml.transform.Transformer; 
import javax.xml.transform.TransformerFactory; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult; 
import javax.xml.transform.stream.StreamSource; 

import org.w3c.dom.Document; 

    class XMLtoCsVConversion { 

     public static void main(String args[]) throws Exception { 
      File stylesheet = new File("style.xsl"); 
      File xmlSource = new File("sample_data.xml"); 

      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder builder = factory.newDocumentBuilder(); 
      Document document = builder.parse(xmlSource); 

      StreamSource stylesource = new StreamSource(stylesheet); 
      Transformer transformer = TransformerFactory.newInstance() 
        .newTransformer(stylesource); 
      Source source = new DOMSource(document); 
      Result outputTarget = new StreamResult(new File("/tmp/x.csv")); 
      transformer.transform(source, outputTarget); 
     } 
    } 

내가 사용하고있는 XSLT입니다 : 여기

는 자바 코드입니다

예상 출력 :

classroom id, classroom_name, teacher_1_id, teacher_1_last_name, teacher_1_first_name, teacher_2_id, teacher_2_last_name, teacher_2_first_name, student_id, student_last_name, student_first_name, grade 
101, Mrs. Jones' Math Class, 10100000001, Jones, Barbara, , , , 10100000010, Gil, Michael, 2 
101, Mrs. Jones' Math Class, 10100000001, Jones, Barbara, , , , 10100000011, Gutierrez, Kimberly, 2 
101, Mrs. Jones' Math Class, 10100000001, Jones, Barbara, , , , 10100000013, Mercado, Toby, 1 
101, Mrs. Jones' Math Class, 10100000001, Jones, Barbara, , , , 10100000014, Garcia, Lizzie, 1 
101, Mrs. Jones' Math Class, 10100000001, Jones, Barbara, , , , 10100000015, Cruz, Alex, 1 
102, Mr. Smith's PhysEd Class, 10200000001, Smith, Arthur, 10200000011, Patterson, John, 10200000010, Smith, Nathaniel, 1 
102, Mr. Smith's PhysEd Class, 10200000001, Smith, Arthur, 10200000011, Patterson, John, 10200000011, McCrancy, Brandon, 1 
102, Mr. Smith's PhysEd Class, 10200000001, Smith, Arthur, 10200000011, Patterson, John, 10200000012, Marco, Elizabeth, 1 
102, Mr. Smith's PhysEd Class, 10200000001, Smith, Arthur, 10200000011, Patterson, John, 10200000013, Lanni, Erica, 1 
102, Mr. Smith's PhysEd Class, 10200000001, Smith, Arthur, 10200000011, Patterson, John, 10200000014, Flores, Michael, 1 
102, Mr. Smith's PhysEd Class, 10200000001, Smith, Arthur, 10200000011, Patterson, John, 10200000015, Hill, Jasmin, 1 
102, Mr. Smith's PhysEd Class, 10200000001, Smith, Arthur, 10200000011, Patterson, John, 10200000016, Perez, Brittany, 1 
102, Mr. Smith's PhysEd Class, 10200000001, Smith, Arthur, 10200000011, Patterson, John, 10200000017, Hiram, William, 1 
102, Mr. Smith's PhysEd Class, 10200000001, Smith, Arthur, 10200000011, Patterson, John, 10200000018, Reginald, Alexis, 1 
102, Mr. Smith's PhysEd Class, 10200000001, Smith, Arthur, 10200000011, Patterson, John, 10200000019, Gayle, Matthew, 1 
103, Brian's Homeroom, 10300000001, O'Donnell, Brian, , , , , , , 

그러나 나는 단지 교실 교실 교실

사람이를 통해 저를 도와 주실 수

는 무엇입니까?

P. 나는 이미 stackoverflow에서 XML 변환에 대한 CSV에 관한 다른 질문을 언급했다. XSL을 만드는 데 도움이되는 게시물에 제공된 정보를 사용했습니다.

+0

예상 출력을 읽을 수 없습니다. 코드 형식으로 게시하십시오. –

+0

해당 출력은 CSV (쉼표로 분리 된 값)가 아니고 탭으로 구분 된 값입니다. 시각적으로 탭 문자를 표시하도록 업데이트하여 진행 상황에 대한 단서를 제공합니다. 나는 당신이 줄을 감쌌다 고 믿는다. 왜냐하면 그것은 여전히 ​​옳지 않은 것처럼 보이기 때문이다. – Andreas

+0

@ michael.hor257k : 지적 해 주셔서 감사합니다. 쉼표가 없도록 Excel에서 예상 형식을 작성했습니다. 그 죄송합니다. 각 값을 구분하는 쉼표로 적절한 CSV 파일을 반영하도록 질문을 편집했습니다. –

답변

2

나는 당신이 당신의 출발점으로 이것을 사용하는 것이 좋습니다 :

XSLT 귀하의 의견에 적용 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="text" encoding="UTF-8"/> 

<xsl:template match="/school"> 
    <!-- header --> 
    <xsl:text>classroom id,classroom_name,teacher_1_id,teacher_1_last_name,teacher_1_first_name,teacher_2_id,teacher_2_last_name,teacher_2_first_name,student_id,student_last_name,student_first_name,grade&#10;</xsl:text> 
    <!-- data --> 
    <xsl:for-each select="grade/classroom"> 
     <!-- classroom data --> 
     <xsl:variable name="classroom-data"> 
      <xsl:value-of select="@id" /> 
      <xsl:text>,</xsl:text> 
      <xsl:value-of select="@name" /> 
      <xsl:text>,</xsl:text> 
      <xsl:value-of select="teacher[1]/@id" /> 
      <xsl:text>,</xsl:text> 
      <xsl:value-of select="teacher[1]/@last_name" /> 
      <xsl:text>,</xsl:text> 
      <xsl:value-of select="teacher[1]/@first_name" /> 
      <xsl:text>,</xsl:text> 
      <xsl:value-of select="teacher[2]/@id" /> 
      <xsl:text>,</xsl:text> 
      <xsl:value-of select="teacher[2]/@last_name" /> 
      <xsl:text>,</xsl:text> 
      <xsl:value-of select="teacher[2]/@first_name" /> 
      <xsl:text>,</xsl:text> 
     </xsl:variable> 
     <xsl:variable name="grade-id" select="../@id" /> 
     <xsl:for-each select="student"> 
      <xsl:copy-of select="$classroom-data"/> 
      <!-- student data --> 
      <xsl:value-of select="@id" /> 
      <xsl:text>,</xsl:text> 
      <xsl:value-of select="@last_name" /> 
      <xsl:text>,</xsl:text> 
      <xsl:value-of select="@first_name" /> 
      <xsl:text>,</xsl:text> 
      <xsl:value-of select="$grade-id" /> 
      <xsl:text>&#10;</xsl:text> 
     </xsl:for-each> 
    </xsl:for-each> 
</xsl:template> 

</xsl:stylesheet> 

, 그 결과는 다음과 같습니다

classroom id,classroom_name,teacher_1_id,teacher_1_last_name,teacher_1_first_name,teacher_2_id,teacher_2_last_name,teacher_2_first_name,student_id,student_last_name,student_first_name,grade 
101,Mrs. Jones' Math Class,10100000001,Jones,Barbara,,,,10100000010,Gil,Michael,1 
101,Mrs. Jones' Math Class,10100000001,Jones,Barbara,,,,10100000011,Gutierrez,Kimberly,1 
101,Mrs. Jones' Math Class,10100000001,Jones,Barbara,,,,10100000013,Mercado,Toby,1 
101,Mrs. Jones' Math Class,10100000001,Jones,Barbara,,,,10100000014,Garcia,Lizzie,1 
101,Mrs. Jones' Math Class,10100000001,Jones,Barbara,,,,10100000015,Cruz,Alex,1 
102,Mr. Smith's PhysEd Class,10200000001,Smith,Arthur,10200000011,Patterson,John,10200000010,Smith,Nathaniel,1 
102,Mr. Smith's PhysEd Class,10200000001,Smith,Arthur,10200000011,Patterson,John,10200000011,McCrancy,Brandon,1 
102,Mr. Smith's PhysEd Class,10200000001,Smith,Arthur,10200000011,Patterson,John,10200000012,Marco,Elizabeth,1 
102,Mr. Smith's PhysEd Class,10200000001,Smith,Arthur,10200000011,Patterson,John,10200000013,Lanni,Erica,1 
102,Mr. Smith's PhysEd Class,10200000001,Smith,Arthur,10200000011,Patterson,John,10200000014,Flores,Michael,1 
102,Mr. Smith's PhysEd Class,10200000001,Smith,Arthur,10200000011,Patterson,John,10200000015,Hill,Jasmin,1 
102,Mr. Smith's PhysEd Class,10200000001,Smith,Arthur,10200000011,Patterson,John,10200000016,Perez,Brittany,1 
102,Mr. Smith's PhysEd Class,10200000001,Smith,Arthur,10200000011,Patterson,John,10200000017,Hiram,William,1 
102,Mr. Smith's PhysEd Class,10200000001,Smith,Arthur,10200000011,Patterson,John,10200000018,Reginald,Alexis,1 
102,Mr. Smith's PhysEd Class,10200000001,Smith,Arthur,10200000011,Patterson,John,10200000019,Gayle,Matthew,1 

참고이 가정합니다 입력 필드에 쉼표 나 큰 따옴표가 없습니다.

+0

도움을 많이 주셔서 감사합니다 :) 나는 XSLT로 자신을 개선하기 위해 노력할 것입니다. –