Ich habe ein einfaches XML erzeugt. Ich möchte Namespace/Präfix in der XML hinzufügen, gibt es eine Möglichkeit mit PHP DomDocument. Oder muss ich Core-PHP verwenden, um die Tags zu ersetzen und sie in XML neu zu erstellen.Präfix/Namespace zu XML hinzufügen
<?xml version="1.0" encoding="UTF-8"?>
<orderShipment>
<orderData>
<productId>1</productId>
<orderStatus>
<status>Shipped</status>
<statusQuantity>
<amount>1</amount>
</statusQuantity>
<trackingInfo>
<shipDateTime>Fri, 24 Feb 2017</shipDateTime>
<carrierName>UPS</carrierName>
<methodCode>Standard</methodCode>
<trackingNumber>123123123</trackingNumber>
<trackingURL>http://ups.com</trackingURL>
</trackingInfo>
</orderStatus>
</orderData>
</orderShipment>
Erwartete Ausgabe
<?xml version="1.0" encoding="UTF-8"?>
<ns2:orderShipment>
<ns2:orderData>
<ns2:productId>1</ns2:productId>
<ns2:orderStatus>
<ns2:status>Shipped</ns2:status>
<ns2:statusQuantity>
<ns2:amount>1</ns2:amount>
</ns2:statusQuantity>
<ns2:trackingInfo>
<ns2:shipDateTime>Fri, 24 Feb 2017</ns2:shipDateTime>
<ns2:carrierName>UPS</ns2:carrierName>
<ns2:methodCode>Standard</ns2:methodCode>
<ns2:trackingNumber>123123123</ns2:trackingNumber>
<ns2:trackingURL>http://ups.com</ns2:trackingURL>
</ns2:trackingInfo>
</ns2:orderStatus>
</ns2:orderData>
</ns2:orderShipment>
Für alle anderen untergeordneten Elemente muss ich das gleiche Format verwenden, z. B. CreateElementNS. Wie ich sehe, hängt es das Attribut 'xmlns: ns2 =" http: // www.ups.com/WSDL/XOLTWS/Track/v2.0 "an. Was ich nicht in untergeordneten Elementen haben möchte. – Priyank
Ich habe das 'orderData'-Kind hinzugefügt Wie Sie sehen, wird die Namespacedefinition nicht ein zweites Mal angehängt Die zusätzlichen Namespacedefinitionen treten nur dann auf, wenn Sie mit dem Kindknoten arbeiten, bevor Sie ihn an seinen Elternknoten anhängen. – ThW