微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SimpleXML 从特定元素获取内部 XML

如何解决SimpleXML 从特定元素获取内部 XML

我在从一个字段中提取值时遇到问题。此处的示例 XML

<Rule id="xccdf_org.cisecurity.benchmarks_rule_4.1.2_Ensure_that_the_kubelet_service_file_ownership_is_set_to_rootroot" selected="false" weight="1.000000" role="full">
    <title
        xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en">Ensure that the kubelet service file ownership is set to root:root
    </title>
    <description
        xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en">
        <xhtml:p>Ensure that the 
            <xhtml:span class="inline_block">kubelet</xhtml:span> service file ownership is set to 
            <xhtml:span class="inline_block">root:root</xhtml:span>.
        </xhtml:p>
    </description>
</Rule>

我想获取 description 字段的整个值

我的测试:

@Test
public void ruletest() throws Exception {
    Serializer serializer = new Persister();
    File source = new File("E:\\test2.xml");
    Rule rule = serializer.read(Rule.class,source);
    System.out.println("-----------------------------------------------");
    System.out.println("Title: "+rule.title+",Desc: "+rule.description.description);
    System.out.println("-----------------------------------------------");
}

和类:

@Root(strict = false,name = "Rule")
public class Rule {
    @Element
    String title;
    @Element(required = false,type = RuleDescription.class)
    RuleDescription description;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}
@Root(strict = false,name = "description")
public class RuleDescription {
    @Element(name = "p",required = false)
    String description;
}

输出为:

-----------------------------------------------
Title: Ensure that the kubelet service file ownership is set to root:root,Desc: Ensure that the 
-----------------------------------------------

这意味着 SimpleXML 只是读​​取值并在出现在其路径上的第一个标记之前中断。我想获得描述的全部价值,即: Ensure that the <xhtml:span class="inline_block">kubelet</xhtml:span> service file ownership is set to <xhtml:span class="inline_block">root:root</xhtml:span>. 或者甚至没有像 Ensure that the kubelet service file ownership is set to root:root 这样的标签更好,但我只需删除标签即可。

如果可能的话,有人能给我一个提示吗?

解决方法

尝试使用 Simple Framework 的 Convert 类。您可能需要编写自己的 Converter 类来解析上述 XML。下面给出了示例链接

SimpleFramwork XML: Element with Inner Text and Child Elements

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。