解析具有多个标记的文本文件并将其写入另一个文本文件

时间:2014-03-27 17:05:25

标签: java xml text-parsing

需要读取看起来像XML的文本文件。文本文件包含多个XML文件,带有父标记。需要逐行解析文件并需要编写所需子标记的相应元素甚至在父标签中多次重复,在一行中,到另一个文本文件。需要将元素写入父标签后的下一行。我知道如何读取文件并写入文件,但我无法根据要求得到逻辑来阅读它。请帮帮我。

    <parentnode xmlns="http://www.fpml.org/FpML-5/recordkeeping" fpmlVersion="5-5" xmlns:abcde="http://www.abcde.com/ext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.abcde.com/ext /../xmls/XYZ/recordkeeping/abcde-ext.xsd">
<Child1 Child1Scheme="http://www.google.com">1234566546</Child1>
<Child1 Child1Scheme="http://www.fpml.org/coding-scheme/external/UNique">AbcdeXYZ-23243423</Child1>
<country countryScheme="http://www.fpml.org/coding-scheme/external/country-identifier">1030253498</country>
<state stateScheme="http://www.fpml.org/coding-scheme/external/state-identifier">23423423423</state>
</parentnode>

<parentnode xmlns="http://www.fpml.org/FpML-5/recordkeeping" fpmlVersion="5-5" xmlns:abcde="http://www.abcde.com/ext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.abcde.com/ext /../xmls/XYZ/recordkeeping/abcde-ext.xsd">
<Child1 Child1Scheme="http://www.google.com">1234566546</Child1>
<Child1 Child1Scheme="http://www.fpml.org/coding-scheme/external/UNique">AbcdeXYZ-154555</Child1>
<country countryScheme="http://www.fpml.org/coding-scheme/external/country-identifier">1030253498</country>
<state stateScheme="http://www.fpml.org/coding-scheme/external/state-identifier">434343242</state>
</parentnode>

<parentnode xmlns="http://www.fpml.org/FpML-5/recordkeeping" fpmlVersion="5-5" xmlns:abcde="http://www.abcde.com/ext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.abcde.com/ext /../xmls/XYZ/recordkeeping/abcde-ext.xsd">
<Child1 Child1Scheme="http://www.google.com">1234566546</Child1>
<Child1 Child1Scheme="http://www.fpml.org/coding-scheme/external/UNique">AbcdeXYZ-4566545</Child1>
<country countryScheme="http://www.fpml.org/coding-scheme/external/country-identifier">1030253498</country>
<state stateScheme="http://www.fpml.org/coding-scheme/external/state-identifier">2323232323</state>
</parentnode>

1 个答案:

答案 0 :(得分:0)

首先,您必须将文件设为有效的xml:

<?xml version="1.0"?>
<rootnode>
    <parentnode xmlns="http://www.fpml.org/FpML-5/recordkeeping"
        fpmlVersion="5-5" xmlns:abcde="http://www.abcde.com/ext"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.abcde.com/ext /../xmls/XYZ/recordkeeping/abcde-ext.xsd">
        <Child1 Child1Scheme="http://www.google.com">1234566546</Child1>
        <Child1 Child1Scheme="http://www.fpml.org/coding-scheme/external/UNique">AbcdeXYZ-23243423</Child1>
        <country
            countryScheme="http://www.fpml.org/coding-scheme/external/country-identifier">1030253498</country>
        <state stateScheme="http://www.fpml.org/coding-scheme/external/state-identifier">23423423423</state>
    </parentnode>

    <parentnode xmlns="http://www.fpml.org/FpML-5/recordkeeping"
        fpmlVersion="5-5" xmlns:abcde="http://www.abcde.com/ext"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.abcde.com/ext /../xmls/XYZ/recordkeeping/abcde-ext.xsd">
        <Child1 Child1Scheme="http://www.google.com">1234566546</Child1>
        <Child1 Child1Scheme="http://www.fpml.org/coding-scheme/external/UNique">AbcdeXYZ-154555</Child1>
        <country
            countryScheme="http://www.fpml.org/coding-scheme/external/country-identifier">1030253498</country>
        <state stateScheme="http://www.fpml.org/coding-scheme/external/state-identifier">434343242</state>
    </parentnode>

    <parentnode xmlns="http://www.fpml.org/FpML-5/recordkeeping"
        fpmlVersion="5-5" xmlns:abcde="http://www.abcde.com/ext"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.abcde.com/ext /../xmls/XYZ/recordkeeping/abcde-ext.xsd">
        <Child1 Child1Scheme="http://www.google.com">1234566546</Child1>
        <Child1 Child1Scheme="http://www.fpml.org/coding-scheme/external/UNique">AbcdeXYZ-4566545</Child1>
        <country
            countryScheme="http://www.fpml.org/coding-scheme/external/country-identifier">1030253498</country>
        <state stateScheme="http://www.fpml.org/coding-scheme/external/state-identifier">2323232323</state>
    </parentnode>
</rootnode>

然后你可以开始阅读你的文件,只需检查一下。

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ReadXMLFile {

    public static void main(String argv[]) {

        try {
            File fXmlFile = new File("file.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);

            doc.getDocumentElement().normalize();

            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

            NodeList nList = doc.getElementsByTagName("parentnode");

            for (int temp = 0; temp < nList.getLength(); temp++) {

                Node nNode = nList.item(temp);

                System.out.println("Current Element :" + nNode.getNodeName());

                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
                    System.out.println("Staff id : " + eElement.getElementsByTagName("Child1").item(0).getTextContent());
                    System.out.println("First Name : " + eElement.getElementsByTagName("Child1").item(0).getTextContent());
                    System.out.println("Last Name : " + eElement.getElementsByTagName("country").item(0).getTextContent());
                    System.out.println("Nick Name : " + eElement.getElementsByTagName("state").item(0).getTextContent());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}