如何从一个简单的XML创建多个XML记录

时间:2018-12-03 04:25:21

标签: xml groovy

我的输入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); 
    }

它仅创建一个,当我尝试添加第二个元素(地址2)的类似代码时,它不起作用。任何帮助将不胜感激。此外,我也在努力寻找应该如何编写一个函数来创建记录而不是针对每个字段进行记录。 *对不起,我是Groovy的初学者”

1 个答案:

答案 0 :(得分:0)

Groovy markup builder在这里更有用。

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>