首页 > 解决方案 > 如何从一个简单的 XML 创建多个 XML 记录

问题描述

我的输入 XML 是

<?xml version="1.0" encoding="UTF-8"?>
<CompoundEmployee>
   <person>
    <person_id_external>12345</person_id_external>
    <created_on_timestamp>2018-01-21T02:11:17.000Z</created_on_timestamp>
    <date_of_birth>1982-03-25</date_of_birth>
    <last_modified_on>2015-11-13T04:08:45.000Z</last_modified_on>
    <person_id>3231</person_id>
    <personal_information>
      <start_date>2015-11-13</start_date>
      <end_date>9999-12-31</end_date>
      <first_name>Joe</first_name>
      <gender>M</gender>
      <is_overridden>false</is_overridden>
      <last_name>Blogg</last_name>
    </personal_information>
    <address_information>
      <address_type>home</address_type>
      <start_date>2016-11-01</start_date>
      <address1>9870 Fox  Drive</address1>
      <address2>Apt 30099</address2>
      <address4> test value of address 4</address4>
      <city>Michigan</city>
      <country>USA</country>
      <end_date>9999-12-31</end_date>
      <state>MI</state>
      <zip_code>48638</zip_code>
    </address_information>
   </person>
</CompoundEmployee>

我想要以下格式的输出

<?xml version="1.0" encoding="UTF-8"?>
<Recrods>
    <recrod>
        <EmpID>12345</EmpID>
        <FieldName>address1</FieldName>
        <FieldValue>9870 Fox  Drive</FieldValue>
    </record>
    <recrod>
        <EmpID>12345</EmpID>
        <FieldName>address2</FieldName>
        <FieldValue>Apt 30099</FieldValue>
    </record>
    <recrod>
        <EmpID>12345</EmpID>
        <FieldName>city</FieldName>
        <FieldValue>Michigan</FieldValue>
    </record>   
    <recrod>
        <EmpID>12345</EmpID>
        <FieldName>country</FieldName>
        <FieldValue>USA</FieldValue>
    </record>
</Recrods>

基本上“address_information”节点的每个字段都有一个“记录”。我能够创建一个记录,但不确定如何附加多个记录。

SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(is);
private static final String empId = null;   
private static final String ELE_NAME_ROOT = "Records";
private static final String ELE_NAME_RECORD = "record";
private static final String address1 = null;
private static final String address2 = null;
private static final String city = null;

XPath Person;
Person = XPath.newInstance("/*/person");

myElements = Person.selectNodes(doc);

for (Element myElement: myElements) {

empId = myElement.getChildText("person_id_external");
}


AddInfo = XPath.newInstance("/*/person/address_information");

myElements = AddInfo.selectNodes(doc);

for (Element myElement: myElements) {
         address_type=myElement.getChildText("address_type");
        if (address_type == 'home') {
            address1 = myElement.getChildText("address1");
            address2 = myElement.getChildText("address2");
            city = myElement.getChildText("city");

        }
    }


    Document doc1 = new Document();

    //CB_MUNICIP

    if ( address1 != null)
    {

    Element eleRoot = new Element(ELE_NAME_ROOT);
    doc1.setRootElement(eleRoot);

    Element eleRecord = new Element(ELE_NAME_RECORD);
    eleRoot.addContent(eleRecord);
    if( empId != null)
    {
    Element eleField = new Element("EMPID");
    eleField.setText(empId);
    eleRecord.addContent(eleField);
    }

    Element eleField4 = new Element("FieldName");
    eleField4.setText("address1");
    eleRecord.addContent(eleField4);

    Element eleField5 = new Element("FieldValue");
    eleField5.setText(address1);
    eleRecord.addContent(eleField5); 
    }

它只创建一个,当我尝试添加类似的代码第二个元素(address2)时,它不起作用。任何帮助都会受到重视。另外,我正在努力找出我应该如何编写一个创建记录而不是为每个字段执行的函数。* 请原谅我——我是 Groovy 的初学者”

标签: xmlgroovy

解决方案


Groovy 标记生成器在这里更有用。

import groovy.xml.MarkupBuilder


def data = """<?xml version="1.0" encoding="UTF-8"?>
<CompoundEmployee>
   <person>
    <person_id_external>12345</person_id_external>
    <created_on_timestamp>2018-01-21T02:11:17.000Z</created_on_timestamp>
    <date_of_birth>1982-03-25</date_of_birth>
    <last_modified_on>2015-11-13T04:08:45.000Z</last_modified_on>
    <person_id>3231</person_id>
    <personal_information>
      <start_date>2015-11-13</start_date>
      <end_date>9999-12-31</end_date>
      <first_name>Joe</first_name>
      <gender>M</gender>
      <is_overridden>false</is_overridden>
      <last_name>Blogg</last_name>
    </personal_information>
    <address_information>
      <address_type>home</address_type>
      <start_date>2016-11-01</start_date>
      <address1>9870 Fox  Drive</address1>
      <address2>Apt 30099</address2>
      <address4> test value of address 4</address4>
      <city>Michigan</city>
      <country>USA</country>
      <end_date>9999-12-31</end_date>
      <state>MI</state>
      <zip_code>48638</zip_code>
    </address_information>
   </person>
</CompoundEmployee>"""

def compoundEmployee = new XmlParser().parseText(data)

def employeeID = compoundEmployee.person.person_id_external.text()
def address = compoundEmployee.person.address_information[0];


def writer = new StringWriter()
def op = new MarkupBuilder(writer)
op.records() {

    address.children().each { Node myNode ->
        record() {
            empID(employeeID)
            FieldName(myNode.name())
            FieldValue(myNode.text())
        }
    }
}

println(writer.toString())

<records>
  <record>
    <empID>12345</empID>
    <FieldName>address_type</FieldName>
    <FieldValue>home</FieldValue>
  </record>
  <record>
    <empID>12345</empID>
    <FieldName>start_date</FieldName>
    <FieldValue>2016-11-01</FieldValue>
  </record>
  <record>
    <empID>12345</empID>
    <FieldName>address1</FieldName>
    <FieldValue>9870 Fox  Drive</FieldValue>
  </record>
  <record>
    <empID>12345</empID>
    <FieldName>address2</FieldName>
    <FieldValue>Apt 30099</FieldValue>
  </record>
  <record>
    <empID>12345</empID>
    <FieldName>address4</FieldName>
    <FieldValue> test value of address 4</FieldValue>
  </record>
  <record>
    <empID>12345</empID>
    <FieldName>city</FieldName>
    <FieldValue>Michigan</FieldValue>
  </record>
  <record>
    <empID>12345</empID>
    <FieldName>country</FieldName>
    <FieldValue>USA</FieldValue>
  </record>
  <record>
    <empID>12345</empID>
    <FieldName>end_date</FieldName>
    <FieldValue>9999-12-31</FieldValue>
  </record>
  <record>
    <empID>12345</empID>
    <FieldName>state</FieldName>
    <FieldValue>MI</FieldValue>
  </record>
  <record>
    <empID>12345</empID>
    <FieldName>zip_code</FieldName>
    <FieldValue>48638</FieldValue>
  </record>
</records>

推荐阅读