使用JSON将XML转换为Java中的JSON

问题描述 投票:0回答:1

有哪些不错的工具可以在Java中轻松将XML转换为JSON?我尝试使用JSON库将XML转换为JSON,但我在堆栈末端有需要选择的内容

这是xml hierarchical data

    -document
     - page
       -block as Array
        [ - text
            - par
              - line 
                - formatting
                 - charParams As array
                   [  all attribute and data]
                   [  all attribute and data]
                   ..........
                   .......... 
]



<!-- https://mvnrepository.com/artifact/org.json/json -->
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20180813</version>
        </dependency>

我的XML FILE格式

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<document xmlns="http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml" version="1.0" producer="ABBYY FineReader Engine 12" languages="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml">
<page width="1600" height="900" resolution="90" originalCoords="1">
<block blockType="Text" blockName="" l="563" t="1" r="822" b="24"><region><rect l="563" t="1" r="586" b="2"/><rect l="563" t="2" r="611" b="3"/><rect l="563" t="3" r="676" b="4"/><rect l="563" t="4" r="721" b="5"/><rect l="563" t="5" r="822" b="6"/><rect l="563" t="6" r="822" b="19"/><rect l="586" t="19" r="822" b="20"/><rect l="613" t="20" r="822" b="21"/><rect l="643" t="21" r="822" b="22"/><rect l="724" t="22" r="822" b="23"/><rect l="791" t="23" r="800" b="24"/></region>
<text>
<par lineSpacing="1260">
<line baseline="34" l="562" t="0" r="820" b="23"><formatting lang="EnglishUnitedStates">
<charParams l="562" t="5" r="572" b="16">A</charParams>
<charParams l="572" t="4" r="581" b="17">C</charParams>
<charParams l="582" t="5" r="585" b="16" suspicious="1">I</charParams>
<charParams l="585" t="5" r="590" b="16"> </charParams>
<charParams l="590" t="5" r="597" b="16">E</charParams>
<charParams l="598" t="5" r="610" b="17">M</charParams>
<charParams l="610" t="5" r="614" b="17"> </charParams>
<charParams l="614" t="5" r="624" b="16">G</charParams>
<charParams l="624" t="6" r="633" b="17">X</charParams>
<charParams l="633" t="5" r="638" b="17"> </charParams>
<charParams l="638" t="5" r="645" b="17">3</charParams>
<charParams l="645" t="14" r="647" b="17">.</charParams>
<charParams l="648" t="4" r="656" b="17">8</charParams>
<charParams l="657" t="5" r="669" b="16">M</charParams>
<charParams l="670" t="11" r="674" b="12">-</charParams>
<charParams l="675" t="4" r="679" b="16"> </charParams>
<charParams l="679" t="4" r="688" b="16">C</charParams>
<charParams l="688" t="7" r="697" b="16">o</charParams>
<charParams l="698" t="8" r="705" b="16">n</charParams>
<charParams l="706" t="8" r="713" b="16">n</charParams>
<charParams l="713" t="8" r="721" b="16">e</charParams>
<charParams l="721" t="8" r="729" b="16">c</charParams>
<charParams l="728" t="6" r="734" b="16">t</charParams>
<charParams l="733" t="8" r="741" b="16">e</charParams>
<charParams l="741" t="5" r="750" b="16">d</charParams>
<charParams l="750" t="5" r="754" b="16"> </charParams>
<charParams l="754" t="7" r="759" b="17">t</charParams>
<charParams l="759" t="8" r="768" b="17">o</charParams>
<charParams l="768" t="5" r="772" b="17"> </charParams>
<charParams l="772" t="5" r="780" b="17">S</charParams>
<charParams l="780" t="8" r="788" b="17">e</charParams>
<charParams l="789" t="8" r="793" b="16">r</charParams>
<charParams l="792" t="7" r="801" b="17">v</charParams>
<charParams l="800" t="7" r="808" b="16">e</charParams>
<charParams l="809" t="8" r="814" b="16" suspicious="1">r</charParams>
<charParams l="817" t="8" r="820" b="16">:</charParams></formatting></line></par>
</text>
</block>
<block blockType="Text" blockName="" l="826" t="0" r="1037" b="29"><region><rect l="826" t="0" r="987" b="1"/><rect l="826" t="1" r="1037" b="23"/><rect l="831" t="23" r="1037" b="24"/><rect l="869" t="24" r="1037" b="25"/><rect l="905" t="25" r="1037" b="26"/><rect l="930" t="26" r="1037" b="27"/><rect l="967" t="27" r="1037" b="28"/><rect l="998" t="28" r="1037" b="29"/></region>
<text>
<par lineSpacing="1260">
<line baseline="28" l="826" t="0" r="1035" b="22"><formatting lang="EnglishUnitedStates">
<charParams l="826" t="5" r="832" b="16">E</charParams>
<charParams l="833" t="5" r="845" b="16">M</charParams>
<charParams l="845" t="5" r="855" b="17">G</charParams>
<charParams l="855" t="5" r="865" b="16">X</charParams>
<charParams l="865" t="5" r="872" b="16">3</charParams>
<charParams l="872" t="5" r="881" b="16">8</charParams>
<charParams l="881" t="4" r="893" b="17">M</charParams>
<charParams l="894" t="11" r="899" b="13">-</charParams>
<charParams l="900" t="8" r="912" b="16">m</charParams>
<charParams l="912" t="8" r="918" b="17">s</charParams>
<charParams l="918" t="8" r="926" b="17">a</charParams>
<charParams l="927" t="8" r="934" b="16">n</charParams>
<charParams l="935" t="5" r="941" b="17">1</charParams>
<charParams l="941" t="5" r="947" b="20"> </charParams>
<charParams l="947" t="5" r="950" b="19" suspicious="1">1</charParams>
<charParams l="955" t="5" r="961" b="16" suspicious="1">1</charParams>
<charParams l="962" t="4" r="970" b="16">7</charParams>
<charParams l="971" t="4" r="978" b="17">2</charParams>
<charParams l="979" t="5" r="989" b="16">2</charParams>
<charParams l="989" t="5" r="997" b="16">9</charParams>
<charParams l="998" t="5" r="1008" b="16">3</charParams>
<charParams l="1009" t="5" r="1016" b="16">5</charParams>
<charParams l="1017" t="5" r="1027" b="16">3</charParams>
<charParams l="1027" t="5" r="1035" b="16">0</charParams></formatting></line></par>
</text>
</block>
<block blockType="Text" blockName="" l="6" t="87" r="194" b="107"><region><rect l="6" t="87" r="194" b="88"/><rect l="6" t="88" r="194" b="105"/><rect l="6" t="105" r="193" b="106"/><rect l="44" t="106" r="74" b="107"/></region>
<text>
<par lineSpacing="1260">
<line baseline="121" l="7" t="86" r="192" b="105"><formatting lang="EnglishUnitedStates">
<charParams l="7" t="87" r="20" b="100" suspicious="1">»</charParams>
<charParams l="21" t="88" r="25" b="101"> </charParams>
<charParams l="26" t="89" r="32" b="101">E</charParams>
<charParams l="33" t="92" r="41" b="103">q</charParams>
<charParams l="43" t="92" r="49" b="101">u</charParams>
<charParams l="51" t="89" r="53" b="100">i</charParams>
<charParams l="55" t="92" r="62" b="103">p</charParams>
<charParams l="64" t="92" r="75" b="101">m</charParams>
<charParams l="76" t="92" r="84" b="100">e</charParams>
<charParams l="85" t="92" r="92" b="100">n</charParams>
<charParams l="92" t="90" r="97" b="100">t</charParams>
<charParams l="98" t="89" r="100" b="100"> </charParams>
<charParams l="101" t="89" r="109" b="100">C</charParams>
<charParams l="110" t="92" r="118" b="100">o</charParams>
<charParams l="120" t="92" r="127" b="100">n</charParams>
<charParams l="127" t="89" r="133" b="100">f</charParams>
<charParams l="133" t="89" r="135" b="100">i</charParams>
<charParams l="136" t="92" r="144" b="104">g</charParams>
<charParams l="146" t="93" r="152" b="101">u</charParams>
<charParams l="154" t="92" r="158" b="100">r</charParams>
<charParams l="158" t="92" r="165" b="101">a</charParams>
<charParams l="166" t="91" r="171" b="101">t</charParams>
<charParams l="172" t="90" r="174" b="101">i</charParams>
<charParams l="175" t="92" r="183" b="101">o</charParams>
<charParams l="185" t="93" r="192" b="101">n</charParams></formatting></line></par>
</text>
</block>
<block blockType="Picture" blockName="" l="298" t="860" r="486" b="900"><region><rect l="484" t="860" r="486" b="861"/><rect l="365" t="861" r="486" b="862"/><rect l="298" t="862" r="486" b="898"/><rect l="298" t="898" r="480" b="899"/><rect l="298" t="899" r="367" b="900"/></region>
</block>
<block blockType="Picture" blockName="" l="1524" t="1" r="1552" b="22"><region><rect l="1524" t="1" r="1552" b="22"/></region>
</block>
<block blockType="Picture" blockName="" l="253" t="865" r="281" b="895"><region><rect l="253" t="865" r="281" b="895"/></region>
</block>
<block blockType="Picture" blockName="" l="1531" t="788" r="1586" b="807"><region><rect l="1532" t="788" r="1586" b="789"/><rect l="1531" t="789" r="1586" b="807"/></region>
</block>
</page>
</document>

JAVA代码

这是我尝试使用JSON库实现Java代码的方式,它在所有节点之前都可以正常工作,但最终我如何选择数据作为该节点的内容或文本***

public static void main(String[] args) {

        try {

            JSONObject xmlJsonObject = XML.toJSONObject(xml_String);

            JSONObject document = (JSONObject) xmlJsonObject.get("document");
            JSONObject page = (JSONObject) document.get("page");
            JSONArray blockArray = (JSONArray) page.get("block");
            blockArray.forEach(arr -> {

                JSONObject arrObj = (JSONObject) arr;
                JSONObject text = (JSONObject) arrObj.get("text");
                JSONObject par = (JSONObject) text.get("par");
                JSONObject line = (JSONObject) par.get("line");
                JSONObject formatting = (JSONObject) line.get("formatting");
                JSONArray charParamsArray = (JSONArray) formatting.get("charParams");

                charParamsArray.forEach(charParamObj -> {

                    JSONObject charParam = (JSONObject) charParamObj;
                    System.out.print(charParamObj);

                //  System.out.print(charParam.get("content"));

                });
                System.out.println("");

                // String string = blockArray.toString(4);
                // System.out.println(arr);

            });

        } catch (Exception e) {
            System.out.println(e.toString());
        }

    }
java json xml parsing
1个回答
0
投票
public class XMLToJson {

    public static void main(String[] args) {
        String xmlString = "XML_FILE_CONTENT_HERE";
        try {
            JSONObject xmlJSONObj = XML.toJSONObject(xmlString);
            int PRETTY_PRINT_INDENT_FACTOR = 4;
            String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
            System.out.println(jsonPrettyPrintString);
        } catch (JSONException je) {
            System.out.println(je.toString());
        }
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.