我发现很多帖子描述了不同XML解析器的优点,但我的问题非常具体。
我的输入XML文件中有各种各样的标记:
project(":android") {
apply plugin: "android"
configurations { natives }
dependencies {
compile project(":core")
compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
compile "com.badlogicgames.ashley:ashley:$ashleyVersion"
compile "com.badlogicgames.gdx:gdx-ai:$aiVersion"
compile 'com.facebook.android:facebook-android-sdk:4.6.0'
}
}
我目前使用两种方法:
1.<link-protection/>
2.<undocumented><lcd>
3. <message>
*****{with escape characters}</message>
4.<xyz comment= "**hshhshs">, and more
在这两种情况下,开发时间与我处理各种标签的时间相同。
我想知道我可以使用更方便的XML解析器。
答案 0 :(得分:1)
查看XStream - https://x-stream.github.io/。 这个框架非常有用,可以处理XML和JSON。
您还可以使用JAXB(Java Architecture for XML binding)。
以下是我要遵循的步骤:
希望这有帮助。
答案 1 :(得分:1)
您应该查看我的unXml - 库。它从XML文档中选取XPath,并使用Jackson中的映射器返回av json对象(或pojo)。
示例XML
<root>
<id>1</id>
<title>mytitle</title>
</root>
Java中的示例ParserCode
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.nerdforge.unxml.Parsing;
import com.nerdforge.unxml.factory.ParsingFactory;
...
public class MyController {
public ObjectNode getJsonFromXml(String inputXmlString) {
Parsing parsing = ParsingFactory.getInstance().create();
Document document = parsing.xml().document(inputXmlString);
Parser<ObjectNode> parser = parsing.obj("root")
.attribute("id", "id", parsing.number()) // uses a number parser
.attribute("title") // both xpath and json-attribute-key is "title"
.build();
ObjectNode node = parser.apply(document);
return node;
}
}
在ObjectNode中生成Json
{
"id":1,
"title":"mytitle"
}