[英]Reading and updating Multi-Level elements in XML files in Java
I have below xml file I want to change some of attribute that xml file using java code. 我下面有xml文件,我想使用Java代码更改xml文件的某些属性。 We have changed some attribute here through java code
我们通过Java代码在此处更改了一些属性
Below Xml file contains related information. Xml文件下面包含相关信息。
<Order>
<AllowedModifications>
<Modification ModificationType="CHANGE_CUSTOM_ATTRIBUTES" ThroughOverride="Y"/>
<Modification ModificationType="RECEIVING_NODE" ThroughOverride="Y"/>
<Modification ModificationType="OTHERS" ThroughOverride="Y"/>
</AllowedModifications>
</Order>
Like if ModificationType=OTHERS then we have to change ThroughOverride value =Y. 就像ModificationType = OTHERS一样,我们必须更改ThroughOverride值= Y。 How we can do it with the help of java code.
我们如何借助Java代码来做到这一点。
I tried below code but it is not changing. 我尝试下面的代码,但它没有改变。
Element eleAllowedModifications = Util.getChildElement(eleOrderRoot,"AllowedModifications")
System.out.println("First Element "+eleAllowedModifications.getNodeName());
Node staff = xmlFile.getElementsByTagName("Modification").item(0);
NamedNodeMap attr = staff.getAttributes();
for (int i = 0; i < attr.getLength(); i++) {
Node nodeAttr = attr.getNamedItem("ModificationType");
MoficationTYp =String.valueOf(nodeAttr);
Node nodeAttr1= attr.getNamedItem("ThroughOverride");
MoficationTp =String.valueOf(nodeAttr);
}
if (MoficationTYp=="OTHERS") {
for (int i = 0; i < attr.getLength(); i++) {
Node nodeAttr = attr.getNamedItem("ThroughOverride");
MoficationTYp =String.valueOf(nodeAttr);
nodeAttr.setNodeValue("Y");
}
}
Here it is not changing that value of ThroughOverride attribute . 在这里,它不会更改ThroughOverride属性的值。
If you use an XML library (Disclosure: I'm affiliated with that project), you can get your result with much less code: 如果您使用XML库 (公开:该项目隶属于我),则可以用更少的代码获得结果:
public class UpdateOrder {
public interface Order {
@XBUpdate("//Modification[@ModificationType='OTHERS']/@ThroughOverride")
void updateOrder(String value);
}
public static void main(String[] args) throws IOException {
Order order = new XBProjector(Flags.TO_STRING_RENDERS_XML).io().url("res://data.xml").read(Order.class);
order.updateOrder("N");
System.out.println(order.toString());
}
}
This program prints out: 该程序将输出:
<Order>
<AllowedModifications>
<Modification ModificationType="CHANGE_CUSTOM_ATTRIBUTES" ThroughOverride="Y"/>
<Modification ModificationType="RECEIVING_NODE" ThroughOverride="Y"/>
<Modification ModificationType="OTHERS" ThroughOverride="N"/>
</AllowedModifications>
</Order>
Well, I'd rater use the following code (for example): 好吧,我会评估者使用以下代码(例如):
Element eleAllowedModifications = Util.getChildElement(eleOrderRoot,"AllowedModifications");
System.out.println("First Element "+ eleAllowedModifications.getNodeName());
Element staff = (Element)xmlFile.getElementsByTagName("Modification").item(0);
moficationTyp = staff.getAttribute("ModificationType");
//moficationTp = String.valueOf(nodeAttr);
if (moficationTyp.equals("OTHERS")) {
staff.setAttribute("ThroughOverride", "Y");
}
Other comments: 其他的建议:
String.valueOf()
does not seems appropriate to me here. String.valueOf()
在这里似乎不适合我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.