从xml String中提取xml标记

时间:2013-07-12 12:49:21

标签: java xml dom

我正在使用网络应用程序。在我的项目中,我使用xml进行动态UI生成。我有一个xml,我想将其转换为xsl,以便我可以将其用于UI。 我的问题是,

我有一个xml字符串,我该怎么做?

来自这些xml字符串,

<editor_tab label="Editor">
        <attributes>
            <attribute sequenceNo="12" dataType="string" controltype="Editor"
                name="additional_info" UILabel="Notes" mandatory="N" default="NA"
                updatable="Y" viewable="Y" attribute_id="END12" />
        </attributes>
    </editor_tab>
    <document_tab label="Document">
        <attributes>
            <attributePair displayName="Entity Attributes" name="FileSet1"
                class="entityAttr">
                <attribute sequenceNo="1" dataType="string" controltype="TD"
                    name="Filename" UILabel="" attribute_id="END407" />
                <attribute sequenceNo="2" dataType="string" controltype="Hidden"
                    name="FileBytestream" UILabel="" attribute_id="END408" />
                <attribute sequenceNo="3" dataType="string" controltype="Hidden"
                    name="AllowedType" UILabel="" attribute_id="END409" />
                <attribute sequenceNo="4" dataType="string" controltype="Hidden"
                    name="AllowedMaxSize" UILabel="" attribute_id="END410" />
            </attributePair>

我必须像单独的xml一样提取,

<?xml version="1.0" encoding="UTF-8"?><editor_tab label="Editor">
        <attributes>
            <attribute sequenceNo="12" dataType="string" controltype="Editor"
                name="additional_info" UILabel="Notes" mandatory="N" default="NA"
                updatable="Y" viewable="Y" attribute_id="END12" />
        </attributes>
    </editor_tab>

我正在使用DOM解析器进行xml解析。

3 个答案:

答案 0 :(得分:0)

Document doc = docBuilder.parse(xmlstring)
NodeList editorTab = doc.getElementsByTagName("editor_tab")
Element et = (Element)editorTab.item(0)

足以获取editor_tab。

答案 1 :(得分:0)

您可以通过Java内置的Java API执行此操作。当我学习如何解析XML时,This tutorial给了我很多帮助。我希望它也能帮到你:)。

答案 2 :(得分:0)

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(responseXml));
Document doc = dBuilder.parse(is);

InputSource 来自 org.xml.sax.InputSource

更多信息:http://www.java2s.com/Code/Java/XML/ParseanXMLstringUsingDOMandaStringReader.htm    希望这会有所帮助。