使用属性中的公共节点名称和属性将XML转换为Java对象?

时间:2016-01-11 22:13:02

标签: java xml xml-parsing jaxb xstream

所以我有一个看起来像这样的POJO:

public class ShippingAddress
{
    private String name;
    private String address;
    private String city;
    private String state;
    private String zip;

    //All the getters and setters
}

然而,XML的出现如下:

<ShippingAddress>
    <Data Name="Name">John Doe</Data>
    <Data Name="Address">123 Main Street</Data>
    <Data Name="City">Anytown</Data>
    <Data Name="State">AK</Data>
    <Data Name="Zip">98765</Data>
</ShippingAddress>

我通常使用XStream进行XML解析,但我对此感到困惑。是否有使用JAXB或其他XML解析器的解决方案?或者它可能只是有一个丑陋的解决方案创建一个“数据”对象数组,然后循环它们来创建我有用的POJO?

谢谢!

1 个答案:

答案 0 :(得分:0)

我想VTD-XML可能会对你有所帮助。下面是代码,我没有添加空检查和东西,但它应该能够实现你想要获得的东西。希望这有帮助!

import java.io.File;
import java.io.FileInputStream;

import com.ximpleware.AutoPilot;
import com.ximpleware.VTDGen;
import com.ximpleware.VTDNav;

public class ParseXML {

    private VTDGen vg;
    private VTDNav vn;
    private File f;
    private FileInputStream fis;

    public void parseFile(String inputfile){
        try {
            f = new File(inputfile);
            fis = new FileInputStream(inputfile);
        }catch(Exception e){
            e.printStackTrace();
            return;
        }
        initialize();
        if(vg!=null)
            parse();
    }

    private void initialize(){
        try{
            byte[] ba = new byte[(int)f.length()];
            fis.read(ba);
            vg = new VTDGen();
            vg.setDoc(ba);
            vg.parse(false);
            vn = vg.getNav();
        }catch(Exception e){}
    }

    private void parse(){
        vn.push();
        AutoPilot ap = new AutoPilot();
        ap.bind(vn);
        ap.resetXPath();
        try{
            ap.selectXPath("//ShippingAddress");
            while(ap.evalXPath()!=-1)
                loadData();
        }catch(Exception e){}
        vn.pop();
    }

    private void loadData(){
        vn.push();
        AutoPilot ap = new AutoPilot();
        ap.bind(vn);
        try{
            ShippingAddress sd = new ShippingAddress();
            ap.selectElement("*");
            while(ap.iterate()){
                //checking if current child element is Data
                if(vn.toRawString(vn.getCurrentIndex()).trim().equalsIgnoreCase("Data")){
                    String attributeValue = "";
                    int i = vn.getAttrVal("Name");
                    if(i != -1)
                        attributeValue = vn.toString(i).trim();

                    String elementValue = vn.toNormalizedString(vn.getText());

                    if(attributeValue.trim().equals("Name")){
                        sd.setName(elementValue);
                    }else if(attributeValue.trim().equals("Address")){
                        sd.setAddress(elementValue);
                    }else if(attributeValue.trim().equals("City")){
                        sd.setCity(elementValue);
                    }else if(attributeValue.trim().equals("State")){
                        sd.setState(elementValue);
                    }else if(attributeValue.trim().equals("Zip")){
                        sd.setZip(elementValue);
                    }
                }
            }
            //Add code/logic to save the newly created ShippingAddress sd object

        }catch(Exception e){}
        vn.pop();
    }
}