问题
Continuing my previous question: simplexml_load_file does not recognize <dc:title> tags
How would you go about setting a value for a <dc: element? I managed to print its value but I read online that the XML parser is only for selecting data, not setting it.
EDIT: Using simplexml_load_file() and PHP
What i'm trying to do using PHP basically is to change this <dc:title>test</dc:title> to <dc:title>HELLO</dc:title> for example, and if that tag <dc:title> doesn't exist than add it.
回答1:
a
<dc:element?
As noted in the first answer to the linked question: dc is not an element name. It is a namespace name.
To add an element that is in a namespace use the applicable method (maybe an overload) to specify the namespace. However this depends on the parser you are using (and you don't specify).
Eg. with .NET's LINQ to XML you create an instance of XNamespace from the namespace's URI and then, as it overloads the + operator, you prepend it to the element name:
var ns = new XNamespace(namespaceUri);
var newElement = doc.firstNode.Add(new XElement(ns + "ElementName"));
来源:https://stackoverflow.com/questions/30890764/xml-setting-values-of-namespace-elements