Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.5k views
in Technique[技术] by (71.8m points)

java - SimpleXML get inner XML from particular Element

I have a problem extracting value form one field. Example XML here:

<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>

I would like to get the whole value for description field

My test:

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

And classes:

@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;
}

Output is:

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

which means that SimpleXML just read the value and breaks before first tag which appear on its path. I would like to get the whole value for description which is: 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>. Or even better without tags like Ensure that the kubelet service file ownership is set to root:root but that I can do simply by removing tags.

Could anyone give me a hint if this is possible?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...