我很难使用stax将Persons.xml标记值替换为NULL,然后使用XMLEventWriter将所有输出发送到另一个fakePersons.xml文件。但是,当我尝试在代码下运行时,它只是更改地址标记。我需要所有4个标签的相同行为。 任何帮助将不胜感激。
Persons.xml
<ADocument>
<Stuff>
<StuffA></StuffA>
</Stuff>
<OtherStuff>
<OtherStuff>
<ABC>YOU KAY</ABC>
</OtherStuff>
</OtherStuff>
<Person>
<uuid>11111111-1111-1111-1111-111111111111</uuid>
<firstName>Some </firstName>
<lastName>Person </lastName>
<personID id ="3" name="PID">111111111111</personID>
</Person>
<Person>
<uuid>22222222-2222-2222-2222-222222222222</uuid>
<firstName id ="1" name="ID">Another Person</firstName>
<address>Main St. 2</address>
<personID>222222222222</personID>
</Person>
<Person>
<uuid>33333333-3333-3333-3333-333333333333</uuid>
<firstName>Some</firstName>
<lastName>Person</lastName>
<personID>111111111111</personID>
</Person>
<MoreStuff>
<foo></foo>
<foo>fooo</foo>
<foo><bar></bar></foo>
<foo>
<bar></bar>
<bar/>
<bar>bb</bar>
</foo>
<bar/>
</MoreStuff>
</ADocument>
&#13;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;
public class ReplaceXmlWithFakeUser
{
private static final String PERSON="Person";
private static final String PERSON_ID="personID";
private static final String PERSON_FIRST_NAME="firstName";
private static final String PERSON_LAST_NAME="lastName";
private static final String PERSON_ADDRESS="address";
public static void main(String[] args) throws XMLStreamException, IOException
{
XMLInputFactory inFactory = XMLInputFactory.newInstance();
XMLEventReader eventReader = inFactory.createXMLEventReader(new BufferedInputStream(new FileInputStream("c:\\naeem_xml\\persons.xml")));
XMLOutputFactory factory = XMLOutputFactory.newInstance();
XMLEventWriter writer = factory.createXMLEventWriter(new BufferedOutputStream(new FileOutputStream("c:\\naeem_xml\\fakePersons.xml")));
XMLEventFactory eventFactory = XMLEventFactory.newInstance();
int person_rec_no =0;
XMLEvent event = null;
while (eventReader.hasNext())
{
event = eventReader.nextEvent();
if (event.getEventType() == XMLEvent.START_ELEMENT && event.asStartElement().getName().toString().equalsIgnoreCase(PERSON))
{
writer.add(event);
Person fakePerson=getFakePerson();;
event = eventReader.nextEvent();
while(!(event.getEventType() == XMLEvent.END_ELEMENT && event.asEndElement().getName().toString().equalsIgnoreCase(PERSON)))
{
if(event.getEventType() == XMLEvent.START_ELEMENT && event.asStartElement().getName().toString().equalsIgnoreCase(PERSON_ID))
{
writer.add(event);
writer.add(eventFactory.createCharacters(fakePerson.personId));
event = eventReader.nextEvent();
}
if(event.getEventType() == XMLEvent.START_ELEMENT && event.asStartElement().getName().toString().equalsIgnoreCase(PERSON_FIRST_NAME))
{
writer.add(event);
writer.add(eventFactory.createCharacters(fakePerson.firstName));
event = eventReader.nextEvent();
}
if(event.getEventType() == XMLEvent.START_ELEMENT && event.asStartElement().getName().toString().equalsIgnoreCase(PERSON_LAST_NAME))
{
writer.add(event);
writer.add(eventFactory.createCharacters(fakePerson.lastName));
event = eventReader.nextEvent();
}
if(event.getEventType() == XMLEvent.START_ELEMENT && event.asStartElement().getName().toString().equalsIgnoreCase(PERSON_ADDRESS))
{
writer.add(event);
writer.add(eventFactory.createCharacters(fakePerson.address));
event = eventReader.nextEvent();
}
else
writer.add(event);
event = eventReader.nextEvent();
}
if(event.getEventType() == XMLEvent.END_ELEMENT && event.asEndElement().getName().toString().equalsIgnoreCase(PERSON))
{
writer.add(event);
}
}
else
{
writer.add(event);
}
}
writer.close();
}
private static Person getFakePerson()
{
//create simple fake user...
Person fakePerson = new Person();
fakePerson.personId = null;
fakePerson.firstName = null;
fakePerson.lastName = null;
fakePerson.address = null;
return fakePerson;
}
static class Person
{
String personId;
String firstName;
String lastName;
String address;
}
}
&#13;
fakePersons.xml
<?xml version="1.0" ?><ADocument>
<Stuff>
<StuffA></StuffA>
</Stuff>
<OtherStuff>
<OtherStuff>
<ABC>yada yada</ABC>
</OtherStuff>
</OtherStuff>
<Person>
<uuid>11111111-1111-1111-1111-111111111111</uuid>
<firstName>Some </firstName>
<lastName>Person </lastName>
<personID id="3" name="PID">111111111111</personID>
</Person>
<Person>
<uuid>22222222-2222-2222-2222-222222222222</uuid>
<firstName id="1" name="ID">Another Person</firstName>
<address></address>
<personID>222222222222</personID>
</Person>
<Person>
<uuid>33333333-3333-3333-3333-333333333333</uuid>
<firstName>Some</firstName>
<lastName>Person</lastName>
<personID>111111111111</personID>
</Person>
<MoreStuff>
<foo></foo>
<foo>fooo</foo>
<foo><bar></bar></foo>
<foo>
<bar></bar>
<bar></bar>
<bar>bb</bar>
</foo>
<bar></bar>
</MoreStuff>
</ADocument>
&#13;