将XML转换为JSON忽略属性

时间:2016-10-30 07:43:46

标签: java json xml

我试图在JAVA中将XML转换为JSON,从XML中删除标记属性。

我尝试使用org.json.XML,但它无法满足我的需求。

是否有图书馆可以做我想做的事情?

示例输入:

<?xml version="1.0"?>
<company g="j">
    <staff id="1001">
        <firstname hi="5">jim</firstname>
        <lastname>fox</lastname>
    </staff>
    <staff id="2001">
        <firstname a="7">jay</firstname>
        <details tmp="0">
            <lastname>box</lastname>
            <nickname >fong fong</nickname>
            <salary id="99">200000</salary>
        </details>
    </staff>
</company>

期望的输出:

{
    "company": {
        "staff": [
            {
                "firstname": "jim"
                "lastname": "fox",
            },
            {
                "firstname": "jay",
                "details": {
                    "lastname": "box",
                    "nickname": "fong fong",
                    "salary":"200000",
            }
        ]
    }
}

我尝试了以下内容,但它使用以下属性转换xml:

package my.transform.data.utils;

import java.io.File;
import org.apache.commons.io.FileUtils;
import org.json.XML;
import org.json.JSONObject;

public class JSONObjectConverter {

    public static void main(String[] args) throws Exception {

        String xml = FileUtils.readFileToString(new File("src/main/resources/staff.xml"));
        JSONObject aJson = XML.toJSONObject(xml);
        System.out.println(aJson.toString());

    }

}

有什么建议吗?

3 个答案:

答案 0 :(得分:2)

您需要使用JAXB将xml内容解组为java对象,然后使用该java对象来准备JSON。

JAXB将给定的xml转换为java对象(这称为解组),然后该java对象可用于构成JSON

您可以参考以下代码段:

public class JAXBToJsonConverter {
     public static void main(String[] args) {
        try {  
            //save the company details content to a .xml file
            // and refer the path below
            File file = new File("C:\\myproject\\company.xml");  

            //create the jaxb context and unmarshall
            JAXBContext jaxbContext = JAXBContext.newInstance(Company.class);  

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();  
            Company company= (Company) jaxbUnmarshaller.unmarshal(file);  

            //create the JSON object
            JSONObject json = new JSONObject(company);
            System.out.println(json);
          } catch (JAXBException e) {  
            e.printStackTrace();  
          }  
    }
  }

公司类:

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Company {

    private Staff staff;

    @XmlElement
    public Staff getStaff() {
        return staff;
    }

    public void setStaff(Staff staff) {
        this.staff = staff;
    }
  }

员工班级:

public class Staff {
    private String firstname;
    private String lastname;

    @XmlElement
    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    @XmlElement
    public String getLastname() {
        return lastname;
    }
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
}

详细信息类:

 public class Details {
     private String lastname;
     private String nickname;
     private int salary;

@XmlElement
public String getLastname() {
   return lastname;
}
public void setLastname(String lastname) {
    this.lastname = lastname;
}

 @XmlElement
 public String getNickname() {
     return nickname;
 }
 public void setNickname(String nickname) {
    this.nickname = nickname;
 }

 @XmlElement
 public int getSalary() {
    return salary;
}
public void setSalary(int salary) {
    this.salary = salary;
}
}  
  

我需要更具动感的东西,因为我的xml与众不同   每次结构。

您可以在此处查看使用staxon

的内容

https://github.com/beckchr/staxon/wiki/Converting-XML-to-JSON

答案 1 :(得分:1)

尝试执行XSLT转换,以便在转换之前将XML转换为所需的格式。 (您还可以考虑使用XSLT 3.0 xml-to-json()函数)。

我认为任何通用转换器很可能在没有预处理或后处理的情况下完全按照您的要求进行操作。

答案 2 :(得分:0)

Underscore-java库具有静态方法U.fromXmlWithoutAttributes(string)和U.toJson(object)。我是该项目的维护者。 Live example

import com.github.underscore.lodash.U;
import java.util.Map;

public class JsonConversion {
    @SuppressWarnings("unchecked")
    public static void main(String args[]) {
        String xmlString  = "<?xml version=\"1.0\"?>"
        + "<company g=\"j\">"
        + "    <staff id=\"1001\">"
        + "        <firstname hi=\"5\">jim</firstname>"
        + "        <lastname>fox</lastname>"
        + "    </staff>"
        + "    <staff id=\"2001\">"
        + "        <firstname a=\"7\">jay</firstname>"
        + "        <details tmp=\"0\">"
        + "            <lastname>box</lastname>"
        + "            <nickname >fong fong</nickname>"
        + "            <salary id=\"99\">200000</salary>"
        + "        </details>"
        + "    </staff>"
        + "</company>"; 

        Map<String, Object> map = (Map) U.fromXmlWithoutAttributes(xmlString);  
        System.out.println(U.toJson(map));  
    }
}    

输出:

{
  "company": {
    "staff": [
      {
        "firstname": "jim",
        "lastname": "fox"
      },
      {
        "firstname": "jay",
        "details": {
          "lastname": "box",
          "nickname": "fong fong",
          "salary": "200000"
        }
      }
    ]
  }
}