简体   繁体   English

如何为XML的每个元素添加前缀

[英]How to add prefix to each element of XML

how can I add prefix to each element of my XML. 如何为XML的每个元素添加前缀。

So in general from XML like this: 因此,通常从XML像这样:

<CLASSXml>
    <CalculationIndex>
        <RunDesc>NormalCalc</RunDesc>
    </CalculationIndex>
    <GlobalData>
        <CalcIdent>
            <CalcUId>CLASS:20160105:09411486:000007773000:203:CS:CZK:349074</CalcUId>
            <CalcNo>2454307119</CalcNo>
            <CustomNo>349074</CustomNo>
            <CalcVer>2300</CalcVer>
            <XMLVer>23.00.01</XMLVer>
            <Detail>
                <Current>1123</Current>
                <Last>1152</Last>
            </Detail>
            <ClassBuild>23.00.04.03</ClassBuild>
            <SAXIFVersion>6.2</SAXIFVersion>
            <SystemDat>2016-01-05</SystemDat>
            <TimeStamp>09:41:14.86</TimeStamp>
            <ProdMasterVer>0</ProdMasterVer>
        </CalcIdent>
    </GlobalData>
</CLASSXml>

Make something like this: 做这样的事情:

<ns2:CLASSXml>
    <ns2:CalculationIndex>
        <ns2:RunDesc>NormalCalc</ns2:RunDesc>
    </ns2:CalculationIndex>
    <ns2:GlobalData>
        <ns2:CalcIdent>
            <ns2:CalcUId>CLASS:20160105:09411486:000007773000:203:CS:CZK:349074</ns2:CalcUId>
            <ns2:CalcNo>2454307119</ns2:CalcNo>
            <ns2:CustomNo>349074</ns2:CustomNo>
            <ns2:CalcVer>2300</ns2:CalcVer>
            <ns2:XMLVer>23.00.01</ns2:XMLVer>
            <ns2:Detail>
                <ns2:Current>1123</ns2:Current>
                <ns2:Last>1152</ns2:Last>
            </ns2:detail>
            <ns2:ClassBuild>23.00.04.03</ns2:ClassBuild>
            <ns2:SAXIFVersion>6.2</ns2:SAXIFVersion>
            <ns2:SystemDat>2016-01-05</ns2:SystemDat>
            <ns2:TimeStamp>09:41:14.86</ns2:TimeStamp>
            <ns2:ProdMasterVer>0</ns2:ProdMasterVer>
        </ns2:CalcIdent>
    </ns2:GlobalData>
</ns2:CLASSXml>

I understand there is possible to do something like this with str_replace (as example below) but it is just heavy messy code which is useless if XML become 500 lines long or something similar. 我知道可以使用str_replace做类似的事情(如下例所示),但这只是繁琐的代码,如果XML长度达到500行或类似的东西,这将是无用的。

Any ideas how can I do this simple? 任何想法,我怎么能做到这一点简单?

public function addNameSpace($xml) {
    $xml = str_replace('<CLASSXml>', '<ns2:CLASSXml>', $xml);
    $xml = str_replace('</CLASSXml>', '</ns2:CLASSXml>', $xml);
    $xml = str_replace('<CalculationIndex>', '<ns2:CalculationIndex>', $xml);
    $xml = str_replace('</CalculationIndex>', '</ns2:CalculationIndex>', $xml);

    // What to do?
}

Using regex in PHP, It will be very easy 在PHP中使用regex,这将非常容易

$xmlString = preg_replace( '/(?<=\<)([^\/].*?)(?=\>)/', 'ns2:$1', $xmlString); // for <test>
$xmlString = preg_replace( '/(?<=\<\/)(.*?)(?=\>)/', 'ns2:$1', $xmlString); // for </test>

Where $xmlString is your xml content. 其中$ xmlString是您的xml内容。

Your function 您的职能

public function addNameSpace($xml)
{
    $xml = preg_replace( '/(?<=\<)([^\/].*?)(?=\>)/', 'ns2:$1', $xml );
    $xml = preg_replace( '/(?<=\<\/)(.*?)(?=\>)/', 'ns2:$1', $xml ); 
    .
    .
}

or suggested: for different namespace 或建议:用于其他名称空间

public function addNameSpace($xml, $namespace)
{
    $xml = preg_replace( '/(?<=\<)([^\/].*?)(?=\>)/', $namespace.'$1', $xml );
    $xml = preg_replace( '/(?<=\<\/)(.*?)(?=\>)/', $namespace.'$1', $xml ); 
    .
    .
}

Right now on which technology you are using .. there is a method in spring in which you can add prefix to each element .. 现在,您正在使用哪种技术..春季有一种方法可以在每个元素上添加前缀。

marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapper() {
             @Override
            public String getPreferredPrefix(String arg0, String arg1, boolean arg2) {
                return "ns1";
            }
        });'

Or there is another way to do this 或有另一种方法可以做到这一点

@XmlSchema(
    namespace = "http://www.example.com/a",
    elementFormDefault = XmlNsForm.QUALIFIED,
    xmlns = {
        @XmlNs(prefix="ns1", namespaceURI="http://www.example.com/a")
    }
)  

package authenticator.beans.login;
import javax.xml.bind.annotation.*;

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

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