how can I add prefix to each element of my XML.
So in general from XML like this:
<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.
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
$xmlString = preg_replace( '/(?<=\<)([^\/].*?)(?=\>)/', 'ns2:$1', $xmlString); // for <test>
$xmlString = preg_replace( '/(?<=\<\/)(.*?)(?=\>)/', 'ns2:$1', $xmlString); // for </test>
Where $xmlString is your xml content.
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.*;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.