简体   繁体   English

如何在 JAVA 中的 XML 字符串中添加新属性? 条件:基于父密钥,如果我们需要进行解析或 SAX,我只能使用 JAVA 内部 api

[英]How to add new attribute into XML string in JAVA? Condition: based on parent Key and I can use only JAVA internal api if we need to do parse or SAX

I have a following xml string .我有以下 xml字符串

<aa>
    <bb>
        <cc>
            <cmd>
                <efg sid="C1D7B70D7AF705731B0" mid="C1D7D7AF705731B0" stid="-1" dopt="3">
                    <pqr>
                        <dru fo="1" fps="1" nku="WBECDD6CC37656E6C9" tt="1"/>
                        <dpo drpr="67" dpi="16"/>
                        <dres >
                            <dre dreid="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:2:1" fa="1" dpt= "1" o="0"/>
                        </dres>
                    </pqr>
                </efg>
            </cmd>
        </cc>
    </bb>
</aa>

I need to add "login" attribute inside <efg> tag.我需要在<efg>标记内添加“登录”属性。 So new XML would be所以新的 XML 将是

<aa>
    <bb>
        <cc>
            <cmd>
                <efg sid="C1D7B70D7AF705731B0" login="sdf34234dfs" mid="C1D7D7AF705731B0" stid="-1" dopt="3">
                    <pqr>
                        <dru fo="1" fps="1" nku="WBECDD6CC37656E6C9" tt="1"/>
                        <dpo drpr="67" dpi="16"/>
                        <dres >
                            <dre dreid="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:2:1" fa="1" dpt= "1" o="0"/>
                        </dres>
                    </pqr>
                </efg>
            </cmd>
        </cc>
    </bb>
</aa>

Condition is:条件是:

  1. I can only use inbuilt Java API (java 8) or SAX parser or xmlbuilder我只能使用内置 Java API (java 8) 或 SAX 解析器或 xmlbuilder
  2. Add condition is based on Parent tag ie need to check <cmd> then in child need to add <login> because it is not sure always that <efg> tag would always be there with the same name, it could be with any name.添加条件基于父标记,即需要检查<cmd>然后在子标记中添加<login>因为不确定<efg>标记是否总是具有相同的名称,它可能具有任何名称。

I have tried with DOM parser with following code.我已经尝试使用以下代码使用 DOM 解析器。

String xml = "xmlString";
        //Use method to convert XML string content to XML Document object
        Document doc = convertStringToXML( xml );
        doc.getDocumentElement().normalize();

        Node m = doc.getElementsByTagName("cmd").item(0).getFirstChild();
        Attr login = doc.createAttribute("login");
        login.setValue("123567");
        m.appendChild(login);

However, I am getting following error in my last line of code.但是,我在最后一行代码中遇到了以下错误。

Exception in thread "main" org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted.

Please anyone suggest me, how to add new attribute login into based on my condition no 2.请任何人建议我,如何根据我的条件 2 添加新属性login

NodeList nodeList = doc.getElementsByTagName("cmd");
                    //Check <cmd> tag is present and then check <cmd> tag has child nodes
                    if (nodeList != null && nodeList.item(0).hasChildNodes()) {
                        //Get first child node of <cmd> xml tag
                        String nodeName = doc.getElementsByTagName("cmd").item(0).getFirstChild().getNodeName();

                        NodeList childNodeList = doc.getElementsByTagName(nodeName);
                        Element el = (Element) childNodeList.item(0);
                        //set pgd_login attribute with respective value
                        el.setAttribute("login", "xyz");

                        //Convert back into xml string from Document
                        xml = XMLHelpers.TransformDOMDocumentToString(doc);
                    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM