Modify XML and remove old nodes with VTD-XML

回眸只為那壹抹淺笑 提交于 2019-12-23 01:03:18

问题


I have a structure like this:

<Hotels>
  <Hotel>
    <Name>Hotel 1</Name>
    <Phone1>11111-1</Phone1>
    <Phone2>11111-2</Phone2>
  </Hotel>
  <Hotel>
    <Name>Hotel 2</Name>
    <Phone1>22222-1</Phone1>
    <Phone2>22222-2</Phone2>
  </Hotel>
  ...
</Hotels>

And I want to modify every phone number to have a structure like this:

<Phone>
  <Number>11111-1</Number>
</Phone>

I'm extracting the numbers and building the new nodes. But I don't know at what point I should remove the old Phone1 and Phone2 nodes.

My code:

VTDGen vg = new VTDGen();
vg.setDoc(bytes);
vg.parse(true);
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);

String path = "/Hotels/Hotel";

ap.selectXPath(path);

List<StringBuilder> newNodes = new ArrayList<StringBuilder>();

int count = 0;
// first while-loop: extract all numbers and build new nodes
while(ap.evalXPath() != -1) {
  StringBuilder newNode = new StringBuilder();

  for(int i = 1; i <= 2; i++) { // 2 times, because of 2 phone numbers in every hotel
    VTDNav vn2 = vn.cloneNav();
    AutoPilot ap2 = new AutoPilot(vn2);

    // extract the phone number
    ap2.selectXPath("Phone" + i);

    if(ap2.evalXPath() == -1) { continue; }

    int textIndex = vn2.getText();
    if(textIndex == -1) { continue; }

    String phone = vn2.toString(textIndex);

    newNode.append("<Phone>")
           .append("<Number>").append(phone).append("</Number>")
           .append("</Phone>");
  }

  newNodes.add(count, newNode);
  count++;
}

// now it's time to modify the xml
XMLModifier xm = new XMLModifier(vn);

ap.selectXPath(path);

count = 0;
// second while-loop: insert the created nodes
while(ap.evalXPath() != -1) {
  xm2.insertBeforeTail(newNodes.get(count).toString());

  count++;
}

VTDNav modifiedVn = xm.outputAndReparse();

I split the extracting and the inserting into two while-loops, because @vtd-xml-author said here it's more efficient to reparse just once.

I'm not able to find out where to remove the old Phone nodes and there must be a better way of doing this as well, or not?

来源:https://stackoverflow.com/questions/26736471/modify-xml-and-remove-old-nodes-with-vtd-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!