- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试使用 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("");
}
您会在使用 par、line 等时遇到同样的问题。因此,不要使用 if
可能只使用try ... catch
堵塞。
关于java - 使用 JSON 将 XML 转换为 Java 中的类对象以及如何获取数据行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58110974/
初学者 android 问题。好的,我已经成功写入文件。例如。 //获取文件名 String filename = getResources().getString(R.string.filename
我已经将相同的图像保存到/data/data/mypackage/img/中,现在我想显示这个全屏,我曾尝试使用 ACTION_VIEW 来显示 android 标准程序,但它不是从/data/dat
我正在使用Xcode 9,Swift 4。 我正在尝试使用以下代码从URL在ImageView中显示图像: func getImageFromUrl(sourceUrl: String) -> UII
我的 Ubuntu 安装 genymotion 有问题。主要是我无法调试我的数据库,因为通过 eclipse 中的 DBMS 和 shell 中的 adb 我无法查看/data/文件夹的内容。没有显示
我正在尝试用 PHP 发布一些 JSON 数据。但是出了点问题。 这是我的 html -- {% for x in sets %}
我观察到两种方法的结果不同。为什么是这样?我知道 lm 上发生了什么,但无法弄清楚 tslm 上发生了什么。 > library(forecast) > set.seed(2) > tts lm(t
我不确定为什么会这样!我有一个由 spring data elasticsearch 和 spring data jpa 使用的类,但是当我尝试运行我的应用程序时出现错误。 Error creatin
在 this vega 图表,如果我下载并转换 flare-dependencies.json使用以下 jq 到 csv命令, jq -r '(map(keys) | add | unique) as
我正在提交一个项目,我必须在其中创建一个带有表的 mysql 数据库。一切都在我这边进行,所以我只想检查如何将我所有的压缩文件发送给使用不同计算机的人。基本上,我如何为另一台计算机创建我的数据库文件,
我有一个应用程序可以将文本文件写入内部存储。我想仔细看看我的电脑。 我运行了 Toast.makeText 来显示路径,它说:/数据/数据/我的包 但是当我转到 Android Studio 的 An
我喜欢使用 Genymotion 模拟器以如此出色的速度加载 Android。它有非常好的速度,但仍然有一些不稳定的性能。 如何从 Eclipse 中的文件资源管理器访问 Genymotion 模拟器
我需要更改 Silverlight 中文本框的格式。数据通过 MVVM 绑定(bind)。 例如,有一个 int 属性,我将 1 添加到 setter 中的值并调用 OnPropertyChanged
我想向 Youtube Data API 提出请求,但我不需要访问任何用户信息。我只想浏览公共(public)视频并根据搜索词显示视频。 我可以在未经授权的情况下这样做吗? 最佳答案 YouTube
我已经设置了一个 Twilio 应用程序,我想向人们发送更新,但我不想回复单个文本。我只是想让他们在有问题时打电话。我一切正常,但我想在发送文本时显示传入文本,以确保我不会错过任何问题。我正在使用 p
我有一个带有表单的网站(目前它是纯 HTML,但我们正在切换到 JQuery)。流程是这样的: 接受用户的输入 --- 5 个整数 通过 REST 调用网络服务 在服务器端运行一些计算...并生成一个
假设我们有一个名为 configuration.js 的文件,当我们查看内部时,我们会看到: 'use strict'; var profile = { "project": "%Projec
这部分是对 Previous Question 的扩展我的: 我现在可以从我的 CI Controller 成功返回 JSON 数据,它返回: {"results":[{"id":"1","Sourc
有什么有效的方法可以删除 ios 中 CBL 的所有文档存储?我对此有疑问,或者,如果有人知道如何从本质上使该应用程序像刚刚安装一样,那也会非常有帮助。我们正在努力确保我们的注销实际上将应用程序设置为
我有一个 Rails 应用程序,它与其他 Rails 应用程序通信以进行数据插入。我使用 jQuery $.post 方法进行数据插入。对于插入,我的其他 Rails 应用程序显示 200 OK。但在
我正在为服务于发布请求的 API 调用运行单元测试。我正在传递请求正文,并且必须将响应作为帐户数据返回。但我只收到断言错误 注意:数据是从 Azure 中获取的 spec.js const accou
我是一名优秀的程序员,十分优秀!