java xml 属性_Java中的XML属性(XML Attribute In Java)

Java中的XML属性(XML Attribute In Java)

我已经创建了上面的XSD以获得以下XML的输出:

Text

从java编码的角度来看,我理解了元素的创建,如果我也尝试了同样的属性,它也会有一个结束括号。

Element TheMainElement = document.createElement("TheMainElement");

如何在没有它出现在结束括号中的情况下添加属性部分?

I have the created the above XSD for an output of the following XML:

Text

From the java coding perspective, I understand the element creation and if I attempted the same for the attribute too, it would have an end bracket as that too.

Element TheMainElement = document.createElement("TheMainElement");

How can I add the Attribute part without it appearing in the end bracket?

原文:https://stackoverflow.com/questions/35602543

2020-04-12 12:04

满意答案

看看org.w3c.dom.Element#setAttribute(String name, String value)

所以在你的情况下它会

TheMainElement.setAttribute("Attribute1OrAttribute2", "Attribute1");

根据您的需要,您还可以查看org.w3c.dom.Element#setAttributeNode(Attr newAttr) 。 您必须实现Attr接口或使用已经提供某些实现的专用库,例如dom4j ,它还可以帮助您创建操作XML文档。

作为旁注,我会个人不鼓励使用首字母大写的变量,标准是使用较低的驼峰案例TheMainElement -> theMainElement请参阅google java样式指南 ,例如

Have a look at org.w3c.dom.Element#setAttribute(String name, String value)

So in your case it would be

TheMainElement.setAttribute("Attribute1OrAttribute2", "Attribute1");

Depending on your needs, you can also have a look at the org.w3c.dom.Element#setAttributeNode(Attr newAttr). You would have to implement the Attr interface or use a dedicated library that already provides some implementation such as dom4j, which will also help you to create a manipulate XML documents.

As a side note, i would personnally discourage the use of variable with the first letter capitalized , the standard would be to use lower camel case TheMainElement -> theMainElement See the google java style guide for example

2016-02-24

相关问答

我假设你使用org.w3c.dom包... 尝试这样的事情: NodeList birthTimeLst = firstElement.getElementsByTagName("birthTime");

Element birthTime = (Element) birthTimeLst.item(0);

String value = birthTime.getAttribute("value");

i'm assuming your using the org.w3c.dom package...

假设您想获得值“SourceID”: Element el = (get to the element named int);

String value = el.getAttribute("name");

// value should equals "SourceID"

如果您真正想要的是数字2,请使用: String content = el.getTextContent();

// and then convert to integer, or whatever

Assuming yo...

带键的节点的xpath(是否为空): expression="//*[@key]";

或者,为了教学目的:空(@key ='')或不空(字符串(@key)) expression="//*[(@key='')or(string(@key))]";

为了解析DOM,国外有很多例子。 标准代码: DocumentBuilderFactory builderFactory =DocumentBuilderFactory.newInstance();

DocumentBuilder build...

在您的代码中,如果您对项目使用List recentVisualisations ,请将其更改为ArrayList recentVisualisations (当然要确保这是您想要的)。 class属性告诉您正在使用List的实现 UPDATE 你也可以像在这里提到的那样内联: 删除class = attribute 基本上 @Path("recentVisualisations")

@VisualisationList(inline=true)

请注意pass和fail是空标签(标签关闭为/> ),并且它们不包含任何值。 如果您尝试提取count属性的值,则可以尝试以下操作 NodeList orderNode = doc.getElementsByTagName("pass");

String strOrdNo = orderNode .item(0).getAttributes().getNamedItem("count").getNodeValue();

logger.info("pass Value = " + strOrdNo)...

禁用名称空间awarness: DocumentBuilderFactory xmlFact = DocumentBuilderFactory.newInstance();

xmlFact.setNamespaceAware(false);

您的xpath表达式现在应该如下所示: "//channel/image/@href"

如果你需要使用它作为名称空间感知,只需实现自己的NameSpaceContext ,应该如下所示: NamespaceContext ctx = new ItunesN...

这是使用XPath和vtd-xml(我是作者)的代码... import com.ximpleware.*;

import java.io.*;

public class removeAttrNode {

public static void main(String[] s) throws VTDException, Exception{

VTDGen vg = new VTDGen(); ByteArrayOutputStream baos = new ByteArra...

看看org.w3c.dom.Element#setAttribute(String name, String value) 所以在你的情况下它会 TheMainElement.setAttribute("Attribute1OrAttribute2", "Attribute1");

根据您的需要,您还可以查看org.w3c.dom.Element#setAttributeNode(Attr newAttr) 。 您必须实现Attr接口或使用已经提供某些实现的专用库,例如dom4j ,它还可以帮助...

您要更改的行是处理指令,而不是元素,因此您尝试将其作为元素查找都不起作用。 尝试 /processing-instruction(xml-stylesheet)

然后,您可以获取该节点的数据,该数据将是href="Sample.xsl" type="text/xsl" 。 执行适当的字符串操作以查找和更改该字符串中的href伪属性 - 对不起,大多数XML API都没有提供任何帮助,因为就XML而言,PI的数据是未格式化的字符串,即使它通常被构造成类似于属性 - 并将新数据设置回Processi...

DOM不是最容易查询文档并找回找到的元素的最佳API。 学习XPath,这是一个更合适的API,或者自己遍历元素树。 getElementById()只返回具有给定id属性的元素(编辑:在文档DTD或模式中标记为这样)。 它无法按name属性查找。 请参阅Java XML DOM:id属性如何特殊? 详情。 DOM is not the best API to easily query your document and get back found elements. Learn XPath, ...

相关文章

把后台管理系统重新写了一下,登录的时候提示,java.lang.IllegalArgumentExce

...

应工作需要。让我搞多数据源。 要求要我在applicationContext中配置多数据源 将其中

...

xml有哪些解析技术?区别是什么?你在项目中用到了xml技术的哪些方面?如何实现的?用jdom解析xm

...

视频介绍 1_疯狂软件_XML_语法基础 -- XML是可扩展标记语言,在所有大型应用中做信息配置,数

...

关于如下两个描述同一对象的xml <student><name>aaa &lt

...

1. dynamicField 的作用是什么? 如果你需要在schema.xml文件中的<fie

...

Solr 是什么?简而言之,Solr 就是一个基于 Lucene 的 webservice 端,建站搜

...

XML文件大概如下,通过DOM去解析,但是如果元素中含有中文,则无法整体返回字符串,只能返回一个第一个

...

最新问答

如果启用了复制处理程序,请确保将其置于其中一个安全角色之后。 我见过人们做的另一件事是在不同的端口上运行admin。 最好在需要auth的页面上使用SSL,这样你就不会发送明确的密码,因此管理和复制将发生在8443上,而常规查询将在8080上发生。 如果您要签署自己的证书,请查看此有用的SO页面: 如何在特定连接上使用不同的证书? I didn't know that /admin was the context for SOLR admin because /admin does not re

第一:在您的样本中,您有: 但是你在询问 //td[@class=‘CarMiniProfile-TableHeader’] (注意TableHeader中的大写'T')。 xpath区分大小写。 第二:通过查询// td [@ class ='CarMiniProfile-TableHeader'] / td,你暗示你在外部td中有一个'td'元素,而它们是兄弟姐妹。 有很多方法可以在这里获得制作和模型

这是你的答案: http://jsfiddle.net/gPsdk/40/ .preloader-container { position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px; background: #FFFFFF; z-index: 5; opacity: 1; -webkit-transition: all 500ms ease-out;

问题是,在启用Outlook库引用的情况下, olMailItem是一个保留常量,我认为当您将Dim olMailItem as Outlook.MailItem ,这不是问题,但是尝试设置变量会导致问题。 以下是完整的解释: 您已将olMailItem声明为对象变量。 在赋值语句的右侧,在将其值设置为对象的实例之前,您将引用此Object 。 这基本上是一个递归错误,因为你有对象试图自己分配自己。 还有另一个潜在的错误,如果之前已经分配了olMailItem ,这个语句会引发另一个错误(可能是

我建议使用wireshark http://www.wireshark.org/通过记录(“捕获”)设备可以看到的网络流量副本来“监听”网络上发生的对话。 当您开始捕获时,数据量似乎过大,但如果您能够发现任何看起来像您的SOAP消息的片段(应该很容易发现),那么您可以通过右键单击并选择来快速过滤到该对话'关注TCP Stream'。 然后,您可以在弹出窗口中查看您编写的SOAP服务与Silverlight客户端之间的整个对话。 如果一切正常,请关闭弹出窗口。 作为一个额外的好处,wireshar

Android默认情况下不提供TextView的合理结果。 您可以使用以下库并实现适当的aligntment。 https://github.com/navabi/JustifiedTextView Android Does not provide Justified aligntment of TextView By default. You can use following library and achieve proper aligntment. https://github.com/

你的代码适合我: class apples { public static void main(String args[]) { System.out.println("Hello World!"); } } 我将它下载到c:\ temp \ apples.java。 以下是我编译和运行的方式: C:\temp>javac -cp . apples.java C:\temp>dir apples Volume in drive C is HP_PAV

12个十六进制数字(带前导0x)表示48位。 那是256 TB的虚拟地址空间。 在AMD64上阅读wiki(我假设你在上面,对吗?)架构http://en.wikipedia.org/wiki/X86-64 12 hex digits (with leading 0x) mean 48 bits. That is 256 TB of virtual address space. Read wiki on AMD64 (I assume that you are on it, right?) ar

这将取决于你想要的。 对象有两种属性:类属性和实例属性。 类属性 类属性对于类的每个实例都是相同的对象。 class MyClass: class_attribute = [] 这里已经为类定义了MyClass.class_attribute ,您可以使用它。 如果您创建MyClass实例,则每个实例都可以访问相同的class_attribute 。 实例属性 instance属性仅在创建实例时可用,并且对于类的每个实例都是唯一的。 您只能在实例上使用它们。 在方法__init__中定

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值