XML:设置命名空间元素的值 [英] XML: setting values of namespace elements

查看:26
本文介绍了XML:设置命名空间元素的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继续我之前的问题:simplexml_load_file 无法识别 <dc:标题>标签

您将如何为 <dc: 元素设置值?我设法打印了它的值,但我在网上看到 XML 解析器仅用于选择数据,而不是设置数据.

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.

使用 simplexml_load_file() 和 PHP

我尝试使用 PHP 所做的基本上是将这个 <dc:title>test</dc:title> 更改为 <dc:title>HELLO</例如 dc:title>,如果该标签 不存在,则添加它.

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.

推荐答案

一个 元素?

如链接问题的第一个答案所述:dc 不是元素名称.这是一个命名空间名称.

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).

例如.使用 .NET 的 LINQ to XML 创建一个 XNamespace<的实例/a> 来自命名空间的 URI,然后,当它重载 + 运算符时,您将其添加到元素名称之前:

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"));

这篇关于XML:设置命名空间元素的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆