gpt4 book ai didi

java - 使用 JSON 将 XML 转换为 Java 中的类对象以及如何获取数据行

转载 作者:行者123 更新时间:2023-12-02 00:12:39 25 4
gpt4 key购买 nike

我尝试使用 JSON 库将 XML 转换为 JSON,但我得到了堆栈末尾,其中有我需要选择的内容

这是xml分层数据

    -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 文件格式

<?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());
}

}

最佳答案

你的问题有点不清楚,但是当我使用 XML 运行你的代码时,提供了异常 org.json.JSONException: JSONObject["text"] not found.正在被抛出。这是因为 XML 中的最后几个“<block>...</block> ”元素不包含 <text>...</text>元素。由于您专门寻找它们而它们不在那里,因此会引发异常。

您可以通过检查来包装代码以查看 JSONObject 是否存在在尝试检索之前,有您正在寻找的 key ,例如...

JSONObject arrObj = (JSONObject) arr;
if(arrObj.has("text")) {
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;
if (charParam.has("content")) {
System.out.print(charParam.get("content"));
} else {
System.out.print(" ");
}
});
System.out.println("");
}

您会在使用 parline 等时遇到同样的问题。因此,不要使用 if可能只使用try ... catch堵塞。

关于java - 使用 JSON 将 XML 转换为 Java 中的类对象以及如何获取数据行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58110974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com