Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从平面文件读取数据并将其写入xml的最佳方法_Java_Xml_File - Fatal编程技术网

Java 从平面文件读取数据并将其写入xml的最佳方法

Java 从平面文件读取数据并将其写入xml的最佳方法,java,xml,file,Java,Xml,File,我有一个扁平的.txt文件,行中有逗号分隔的值,类似于: 1,name1,department1 2,name2,department2 3,name3,department3 ... ... <Employees> <Employee> <Code>1</Code> <Name>name1</Name> <Department>depart

我有一个扁平的.txt文件,行中有逗号分隔的值,类似于:

1,name1,department1
2,name2,department2
3,name3,department3
...
...
<Employees>
     <Employee>
          <Code>1</Code>
          <Name>name1</Name>
          <Department>department1</Department>
     </Employee>
     <Employee>
          <Code>2</Code>
          <Name>name2</Name>
          <Department>department2</Department>
     </Employee>
     <Employee>
          <Code>3</Code>
          <Name>name3</Name>
          <Department>department3</Department>
     </Employee>
</Employees>
现在,我想从.txt文件中读取这些记录,并将其写入xml,输出应该如下所示:

1,name1,department1
2,name2,department2
3,name3,department3
...
...
<Employees>
     <Employee>
          <Code>1</Code>
          <Name>name1</Name>
          <Department>department1</Department>
     </Employee>
     <Employee>
          <Code>2</Code>
          <Name>name2</Name>
          <Department>department2</Department>
     </Employee>
     <Employee>
          <Code>3</Code>
          <Name>name3</Name>
          <Department>department3</Department>
     </Employee>
</Employees>
因此,为了实现这一点,我已经讨论了各种问题/帖子,不知何故,我对应该遵循的方法以及应该使用哪个XMLBuilder感到困惑,比如XStream


有人能告诉我,为了获得最佳性能,我应该采用哪种方法吗?

以下是psuedocode中最简单的方法:

file.write("<Employees>");
foreach(String line : file)
{
    String[] parts = line.split(",");
    file.write("<Employee><Code>" + parts[0] + "</Code><Name>" + parts[1] + "</Name><Department>" + parts[2] + "</Department></Employee>");
}
file.write("</Employees>");

显然,此解决方案非常简单,假设平面文件的字段中不包含逗号,并且每行正好有3列。

以下是psuedocode中最简单的方法:

file.write("<Employees>");
foreach(String line : file)
{
    String[] parts = line.split(",");
    file.write("<Employee><Code>" + parts[0] + "</Code><Name>" + parts[1] + "</Name><Department>" + parts[2] + "</Department></Employee>");
}
file.write("</Employees>");

显然,此解决方案非常简单,它假定平面文件的字段中不包含逗号,并且每行正好有3列。

从您的评论中,最简单的方法似乎是在没有任何xml生成器使用打印/写入的情况下执行此操作:

  • 逐行读取txt文件
  • 使用“,”作为分隔符拆分字段
  • 使用simple System.out.print方法将xml标记写入文件/stdout
  • 不要忘记XML头


    如果您的格式经常更改,您应该编写一个
    .xsd模式
    ,并使用
    jaxb
    生成类层次结构和编组/解编组代码,但在这种情况下,这将是一种过分的做法。

    从您的评论来看,最简单的方法似乎是在没有任何使用打印/写入的xml生成器的情况下执行此操作:

  • 逐行读取txt文件
  • 使用“,”作为分隔符拆分字段
  • 使用simple System.out.print方法将xml标记写入文件/stdout
  • 不要忘记XML头


    如果您的格式经常更改,您应该编写一个
    .xsd模式
    ,并使用
    jaxb
    来生成类层次结构和封送/解封代码,但在这种情况下,这样做会有点过分。

    我会使用诸如openCSV之类的CSV库来读取文件,然后使用jaxb来创建XML文件

    您可以使用
    列表创建
    Employees
    类,其中
    Employees
    具有字段
    code
    Name
    等。使用CSV库填写该类。使用
    JAXB.marshal
    方法之一,在一行中将整个内容写入文件

    简单示例代码

    @XmlRootElement
    @XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
    public class XmlWriterTest
    {
        public String foo;
        public List<String> bars;
    
        public static void main(String[] args)
        {
            XmlWriterTest test = new XmlWriterTest();
            test.foo = "hi";
            test.bars = Arrays.asList("yo", "oi");
            JAXB.marshal(test, System.out);
        }   
    }
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.PUBLIC_成员)
    公共类XmlWriterTest
    {
    公共字符串foo;
    公共酒吧名单;
    公共静态void main(字符串[]args)
    {
    XmlWriterTest=新的XmlWriterTest();
    test.foo=“hi”;
    test.bar=Arrays.asList(“yo”、“oi”);
    marshal(test,System.out);
    }   
    }
    
    我将使用诸如openCSV之类的CSV库读取文件,然后使用JAXB创建XML文件

    您可以使用
    列表创建
    Employees
    类,其中
    Employees
    具有字段
    code
    Name
    等。使用CSV库填写该类。使用
    JAXB.marshal
    方法之一,在一行中将整个内容写入文件

    简单示例代码

    @XmlRootElement
    @XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
    public class XmlWriterTest
    {
        public String foo;
        public List<String> bars;
    
        public static void main(String[] args)
        {
            XmlWriterTest test = new XmlWriterTest();
            test.foo = "hi";
            test.bars = Arrays.asList("yo", "oi");
            JAXB.marshal(test, System.out);
        }   
    }
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.PUBLIC_成员)
    公共类XmlWriterTest
    {
    公共字符串foo;
    公共酒吧名单;
    公共静态void main(字符串[]args)
    {
    XmlWriterTest=新的XmlWriterTest();
    test.foo=“hi”;
    test.bar=Arrays.asList(“yo”、“oi”);
    marshal(test,System.out);
    }   
    }
    
    单线awk解决方案怎么样

    awk -F, 'BEGIN{printf "<Employees>\n"}END{printf "</Employees>\n"}{printf"<Employee><Code>%s</Code><Name>%s</Name><Department>%s</Department></Employee>\n",$1,$2,$3}' data.txt 
    
    对于这样一个简单的问题,编写一个Java程序似乎有些过分了

    更新 如果要格式化输出,可以通过管道将其传输到xmllint命令中:

    $ awk -F, 'BEGIN{printf "<Employees>"}END{printf "</Employees>"}{printf"<Employee><Code>%s</Code><Name>%s</Name><Department>%s</Department></Employee>",$1,$2,$3}' data.txt | xmllint --format -
    <?xml version="1.0"?>
    <Employees>
      <Employee>
        <Code>1</Code>
        <Name>name1</Name>
        <Department>department1</Department>
      </Employee>
      <Employee>
        <Code>2</Code>
        <Name>name2</Name>
        <Department>department2</Department>
      </Employee>
      <Employee>
        <Code>3</Code>
        <Name>name3</Name>
        <Department>department3</Department>
      </Employee>
    </Employees>
    

    单线awk解决方案如何

    awk -F, 'BEGIN{printf "<Employees>\n"}END{printf "</Employees>\n"}{printf"<Employee><Code>%s</Code><Name>%s</Name><Department>%s</Department></Employee>\n",$1,$2,$3}' data.txt 
    
    对于这样一个简单的问题,编写一个Java程序似乎有些过分了

    更新 如果要格式化输出,可以通过管道将其传输到xmllint命令中:

    $ awk -F, 'BEGIN{printf "<Employees>"}END{printf "</Employees>"}{printf"<Employee><Code>%s</Code><Name>%s</Name><Department>%s</Department></Employee>",$1,$2,$3}' data.txt | xmllint --format -
    <?xml version="1.0"?>
    <Employees>
      <Employee>
        <Code>1</Code>
        <Name>name1</Name>
        <Department>department1</Department>
      </Employee>
      <Employee>
        <Code>2</Code>
        <Name>name2</Name>
        <Department>department2</Department>
      </Employee>
      <Employee>
        <Code>3</Code>
        <Name>name3</Name>
        <Department>department3</Department>
      </Employee>
    </Employees>
    

    首先,你还不需要xml解析器,你需要一个xml构建器。你需要做多少次?还有什么
    吗?你会经常更改xml格式吗?还是它是稳定的/静态的。@artbristol平面文件可能很大,需要每1或2天执行一次此过程…@count0格式将保持不变…首先,你不需要xml解析器,你需要xml生成器。你需要这样做多少次?还有什么
    吗?您会经常更改xml格式吗?还是它是稳定的/静态的。@artbristol平面文件可能很大,需要每1或2天执行一次此过程…@count0格式将保持不变。。。。