Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 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?

question from:https://stackoverflow.com/questions/65836098/simplexml-get-inner-xml-from-particular-element

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

1 Reply

0 votes
by (71.8m points)

Try using Convert class of Simple Framework . You might need to write your own Converter class to parse the above XML.Sample link is given below

SimpleFramwork XML: Element with Inner Text and Child Elements


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...