Remove SVG/XML child element based on value











up vote
0
down vote

favorite












I have an SVG file, my problem is to remove text element if the value is not a integer using VBA. Did a search and found this article but couldn't get it to work for me Remove a parent node and all children based on text value in child



<?xml version="1.0" ?>
<svg>
<image height="1980" width="1530" x="0" xlink:href="file.png" y="0"/>
<g stroke="#000000" stroke-width="1">
<text fill="#000000" font-family="Arial" font-size="15" x="645" y="532">A</text>
<text fill="#000000" font-family="Arial" font-size="19" x="1391" y="603">2</text>
<text fill="#000000" font-family="Arial" font-size="15" x="261" y="689">A</text>
<text fill="#000000" font-family="Arial" font-size="15" x="1008" y="749">4</text>
<text fill="#000000" font-family="Arial" font-size="14" x="1009" y="768">eD</text>
<text fill="#000000" font-family="Arial" font-size="16" x="117" y="765">A</text>
<text fill="#000000" font-family="Arial" font-size="14" x="199" y="1115">Q</text>
<text fill="#000000" font-family="Arial" font-size="19" x="1393" y="1113">3</text>
<text>DRA</text>
<text fill="#000000" font-family="Arial" font-size="19" x="1131" y="1189">(?YPI PL?I CEG)</text>
<text fill="#000000" font-family="Arial" font-size="19" x="340" y="1310">B</text>
<text fill="#000000" font-family="Arial" font-size="19" x="1223" y="1310">6</text>
<text fill="#000000" font-family="Arial" font-size="18" x="1222" y="1364">7</text>

<text fill="#000000" font-family="Arial" font-size="19" x="755" y="1484">(?YAL I6 PL)</text>
</g>
</svg>


Code



Public Sub xml()


Set XMLDoc = _
CreateObject("Microsoft.XMLDOM")
XMLDoc.Async = "False"
XMLDoc.Load ("C:Users1g2.xml")

strVulid = "eD"
XPath = "svg/g[/text = '" & strVulid & "']"

For Each n In XMLDoc.SelectNodes(XPath)
n.ParentNode.RemoveChild (n)
Next

XMLDoc.Save "C:Users1sample.xml"
MsgBox "done"

End Sub









share|improve this question
























  • Can you post the code you have tried so far?
    – TeeKea
    Nov 20 at 15:41










  • added code. please check
    – subbu
    Nov 20 at 15:53










  • What happens when you run it?
    – Tim Williams
    Nov 20 at 16:05










  • Nothing. the sample.xml reproduces the source as is.
    – subbu
    Nov 20 at 16:14















up vote
0
down vote

favorite












I have an SVG file, my problem is to remove text element if the value is not a integer using VBA. Did a search and found this article but couldn't get it to work for me Remove a parent node and all children based on text value in child



<?xml version="1.0" ?>
<svg>
<image height="1980" width="1530" x="0" xlink:href="file.png" y="0"/>
<g stroke="#000000" stroke-width="1">
<text fill="#000000" font-family="Arial" font-size="15" x="645" y="532">A</text>
<text fill="#000000" font-family="Arial" font-size="19" x="1391" y="603">2</text>
<text fill="#000000" font-family="Arial" font-size="15" x="261" y="689">A</text>
<text fill="#000000" font-family="Arial" font-size="15" x="1008" y="749">4</text>
<text fill="#000000" font-family="Arial" font-size="14" x="1009" y="768">eD</text>
<text fill="#000000" font-family="Arial" font-size="16" x="117" y="765">A</text>
<text fill="#000000" font-family="Arial" font-size="14" x="199" y="1115">Q</text>
<text fill="#000000" font-family="Arial" font-size="19" x="1393" y="1113">3</text>
<text>DRA</text>
<text fill="#000000" font-family="Arial" font-size="19" x="1131" y="1189">(?YPI PL?I CEG)</text>
<text fill="#000000" font-family="Arial" font-size="19" x="340" y="1310">B</text>
<text fill="#000000" font-family="Arial" font-size="19" x="1223" y="1310">6</text>
<text fill="#000000" font-family="Arial" font-size="18" x="1222" y="1364">7</text>

<text fill="#000000" font-family="Arial" font-size="19" x="755" y="1484">(?YAL I6 PL)</text>
</g>
</svg>


Code



Public Sub xml()


Set XMLDoc = _
CreateObject("Microsoft.XMLDOM")
XMLDoc.Async = "False"
XMLDoc.Load ("C:Users1g2.xml")

strVulid = "eD"
XPath = "svg/g[/text = '" & strVulid & "']"

For Each n In XMLDoc.SelectNodes(XPath)
n.ParentNode.RemoveChild (n)
Next

XMLDoc.Save "C:Users1sample.xml"
MsgBox "done"

End Sub









share|improve this question
























  • Can you post the code you have tried so far?
    – TeeKea
    Nov 20 at 15:41










  • added code. please check
    – subbu
    Nov 20 at 15:53










  • What happens when you run it?
    – Tim Williams
    Nov 20 at 16:05










  • Nothing. the sample.xml reproduces the source as is.
    – subbu
    Nov 20 at 16:14













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have an SVG file, my problem is to remove text element if the value is not a integer using VBA. Did a search and found this article but couldn't get it to work for me Remove a parent node and all children based on text value in child



<?xml version="1.0" ?>
<svg>
<image height="1980" width="1530" x="0" xlink:href="file.png" y="0"/>
<g stroke="#000000" stroke-width="1">
<text fill="#000000" font-family="Arial" font-size="15" x="645" y="532">A</text>
<text fill="#000000" font-family="Arial" font-size="19" x="1391" y="603">2</text>
<text fill="#000000" font-family="Arial" font-size="15" x="261" y="689">A</text>
<text fill="#000000" font-family="Arial" font-size="15" x="1008" y="749">4</text>
<text fill="#000000" font-family="Arial" font-size="14" x="1009" y="768">eD</text>
<text fill="#000000" font-family="Arial" font-size="16" x="117" y="765">A</text>
<text fill="#000000" font-family="Arial" font-size="14" x="199" y="1115">Q</text>
<text fill="#000000" font-family="Arial" font-size="19" x="1393" y="1113">3</text>
<text>DRA</text>
<text fill="#000000" font-family="Arial" font-size="19" x="1131" y="1189">(?YPI PL?I CEG)</text>
<text fill="#000000" font-family="Arial" font-size="19" x="340" y="1310">B</text>
<text fill="#000000" font-family="Arial" font-size="19" x="1223" y="1310">6</text>
<text fill="#000000" font-family="Arial" font-size="18" x="1222" y="1364">7</text>

<text fill="#000000" font-family="Arial" font-size="19" x="755" y="1484">(?YAL I6 PL)</text>
</g>
</svg>


Code



Public Sub xml()


Set XMLDoc = _
CreateObject("Microsoft.XMLDOM")
XMLDoc.Async = "False"
XMLDoc.Load ("C:Users1g2.xml")

strVulid = "eD"
XPath = "svg/g[/text = '" & strVulid & "']"

For Each n In XMLDoc.SelectNodes(XPath)
n.ParentNode.RemoveChild (n)
Next

XMLDoc.Save "C:Users1sample.xml"
MsgBox "done"

End Sub









share|improve this question















I have an SVG file, my problem is to remove text element if the value is not a integer using VBA. Did a search and found this article but couldn't get it to work for me Remove a parent node and all children based on text value in child



<?xml version="1.0" ?>
<svg>
<image height="1980" width="1530" x="0" xlink:href="file.png" y="0"/>
<g stroke="#000000" stroke-width="1">
<text fill="#000000" font-family="Arial" font-size="15" x="645" y="532">A</text>
<text fill="#000000" font-family="Arial" font-size="19" x="1391" y="603">2</text>
<text fill="#000000" font-family="Arial" font-size="15" x="261" y="689">A</text>
<text fill="#000000" font-family="Arial" font-size="15" x="1008" y="749">4</text>
<text fill="#000000" font-family="Arial" font-size="14" x="1009" y="768">eD</text>
<text fill="#000000" font-family="Arial" font-size="16" x="117" y="765">A</text>
<text fill="#000000" font-family="Arial" font-size="14" x="199" y="1115">Q</text>
<text fill="#000000" font-family="Arial" font-size="19" x="1393" y="1113">3</text>
<text>DRA</text>
<text fill="#000000" font-family="Arial" font-size="19" x="1131" y="1189">(?YPI PL?I CEG)</text>
<text fill="#000000" font-family="Arial" font-size="19" x="340" y="1310">B</text>
<text fill="#000000" font-family="Arial" font-size="19" x="1223" y="1310">6</text>
<text fill="#000000" font-family="Arial" font-size="18" x="1222" y="1364">7</text>

<text fill="#000000" font-family="Arial" font-size="19" x="755" y="1484">(?YAL I6 PL)</text>
</g>
</svg>


Code



Public Sub xml()


Set XMLDoc = _
CreateObject("Microsoft.XMLDOM")
XMLDoc.Async = "False"
XMLDoc.Load ("C:Users1g2.xml")

strVulid = "eD"
XPath = "svg/g[/text = '" & strVulid & "']"

For Each n In XMLDoc.SelectNodes(XPath)
n.ParentNode.RemoveChild (n)
Next

XMLDoc.Save "C:Users1sample.xml"
MsgBox "done"

End Sub






xml vba svg






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 15:52

























asked Nov 20 at 15:33









subbu

11




11












  • Can you post the code you have tried so far?
    – TeeKea
    Nov 20 at 15:41










  • added code. please check
    – subbu
    Nov 20 at 15:53










  • What happens when you run it?
    – Tim Williams
    Nov 20 at 16:05










  • Nothing. the sample.xml reproduces the source as is.
    – subbu
    Nov 20 at 16:14


















  • Can you post the code you have tried so far?
    – TeeKea
    Nov 20 at 15:41










  • added code. please check
    – subbu
    Nov 20 at 15:53










  • What happens when you run it?
    – Tim Williams
    Nov 20 at 16:05










  • Nothing. the sample.xml reproduces the source as is.
    – subbu
    Nov 20 at 16:14
















Can you post the code you have tried so far?
– TeeKea
Nov 20 at 15:41




Can you post the code you have tried so far?
– TeeKea
Nov 20 at 15:41












added code. please check
– subbu
Nov 20 at 15:53




added code. please check
– subbu
Nov 20 at 15:53












What happens when you run it?
– Tim Williams
Nov 20 at 16:05




What happens when you run it?
– Tim Williams
Nov 20 at 16:05












Nothing. the sample.xml reproduces the source as is.
– subbu
Nov 20 at 16:14




Nothing. the sample.xml reproduces the source as is.
– subbu
Nov 20 at 16:14












1 Answer
1






active

oldest

votes

















up vote
0
down vote













The following use xml linq to remove the elements from the XDocument. The code parse all the attributes just incase you need the extra data



Imports System.Xml
Imports System.Xml.Linq
Module Module1
Const FILENAME As String = "c:temptest.xml"
Sub Main()
Dim doc As XDocument = XDocument.Load(FILENAME)
Dim value As Integer
Dim fonts As List(Of Font) = doc.Descendants("text").Select(Function(x) New Font() With { _
.fill = CType(x.Attribute("fill"), String),
.fontFamily = CType(x.Attribute("font-family"), String),
.size = CType(x.Attribute("font-size"), Integer?),
.x = CType(x.Attribute("x"), Integer?),
.y = CType(x.Attribute("y"), Integer?),
.value = CType(x, String),
.element = x
}).ToList()

For i = fonts.Count - 1 To 0 Step -1
If Not Integer.TryParse(fonts(i).value, value) Then
fonts(i).element.Remove()
End If
Next i

End Sub

End Module
Public Class Font
Public element As XElement
Public fill As String
Public fontFamily As String
Public size As Integer?
Public x As Integer?
Public y As Integer?
Public value As String


End Class





share|improve this answer





















  • OP is using VBA...
    – Tim Williams
    Nov 20 at 20:22










  • thanks but looking for a solution in VBA
    – subbu
    Nov 22 at 10:26











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396408%2fremove-svg-xml-child-element-based-on-value%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













The following use xml linq to remove the elements from the XDocument. The code parse all the attributes just incase you need the extra data



Imports System.Xml
Imports System.Xml.Linq
Module Module1
Const FILENAME As String = "c:temptest.xml"
Sub Main()
Dim doc As XDocument = XDocument.Load(FILENAME)
Dim value As Integer
Dim fonts As List(Of Font) = doc.Descendants("text").Select(Function(x) New Font() With { _
.fill = CType(x.Attribute("fill"), String),
.fontFamily = CType(x.Attribute("font-family"), String),
.size = CType(x.Attribute("font-size"), Integer?),
.x = CType(x.Attribute("x"), Integer?),
.y = CType(x.Attribute("y"), Integer?),
.value = CType(x, String),
.element = x
}).ToList()

For i = fonts.Count - 1 To 0 Step -1
If Not Integer.TryParse(fonts(i).value, value) Then
fonts(i).element.Remove()
End If
Next i

End Sub

End Module
Public Class Font
Public element As XElement
Public fill As String
Public fontFamily As String
Public size As Integer?
Public x As Integer?
Public y As Integer?
Public value As String


End Class





share|improve this answer





















  • OP is using VBA...
    – Tim Williams
    Nov 20 at 20:22










  • thanks but looking for a solution in VBA
    – subbu
    Nov 22 at 10:26















up vote
0
down vote













The following use xml linq to remove the elements from the XDocument. The code parse all the attributes just incase you need the extra data



Imports System.Xml
Imports System.Xml.Linq
Module Module1
Const FILENAME As String = "c:temptest.xml"
Sub Main()
Dim doc As XDocument = XDocument.Load(FILENAME)
Dim value As Integer
Dim fonts As List(Of Font) = doc.Descendants("text").Select(Function(x) New Font() With { _
.fill = CType(x.Attribute("fill"), String),
.fontFamily = CType(x.Attribute("font-family"), String),
.size = CType(x.Attribute("font-size"), Integer?),
.x = CType(x.Attribute("x"), Integer?),
.y = CType(x.Attribute("y"), Integer?),
.value = CType(x, String),
.element = x
}).ToList()

For i = fonts.Count - 1 To 0 Step -1
If Not Integer.TryParse(fonts(i).value, value) Then
fonts(i).element.Remove()
End If
Next i

End Sub

End Module
Public Class Font
Public element As XElement
Public fill As String
Public fontFamily As String
Public size As Integer?
Public x As Integer?
Public y As Integer?
Public value As String


End Class





share|improve this answer





















  • OP is using VBA...
    – Tim Williams
    Nov 20 at 20:22










  • thanks but looking for a solution in VBA
    – subbu
    Nov 22 at 10:26













up vote
0
down vote










up vote
0
down vote









The following use xml linq to remove the elements from the XDocument. The code parse all the attributes just incase you need the extra data



Imports System.Xml
Imports System.Xml.Linq
Module Module1
Const FILENAME As String = "c:temptest.xml"
Sub Main()
Dim doc As XDocument = XDocument.Load(FILENAME)
Dim value As Integer
Dim fonts As List(Of Font) = doc.Descendants("text").Select(Function(x) New Font() With { _
.fill = CType(x.Attribute("fill"), String),
.fontFamily = CType(x.Attribute("font-family"), String),
.size = CType(x.Attribute("font-size"), Integer?),
.x = CType(x.Attribute("x"), Integer?),
.y = CType(x.Attribute("y"), Integer?),
.value = CType(x, String),
.element = x
}).ToList()

For i = fonts.Count - 1 To 0 Step -1
If Not Integer.TryParse(fonts(i).value, value) Then
fonts(i).element.Remove()
End If
Next i

End Sub

End Module
Public Class Font
Public element As XElement
Public fill As String
Public fontFamily As String
Public size As Integer?
Public x As Integer?
Public y As Integer?
Public value As String


End Class





share|improve this answer












The following use xml linq to remove the elements from the XDocument. The code parse all the attributes just incase you need the extra data



Imports System.Xml
Imports System.Xml.Linq
Module Module1
Const FILENAME As String = "c:temptest.xml"
Sub Main()
Dim doc As XDocument = XDocument.Load(FILENAME)
Dim value As Integer
Dim fonts As List(Of Font) = doc.Descendants("text").Select(Function(x) New Font() With { _
.fill = CType(x.Attribute("fill"), String),
.fontFamily = CType(x.Attribute("font-family"), String),
.size = CType(x.Attribute("font-size"), Integer?),
.x = CType(x.Attribute("x"), Integer?),
.y = CType(x.Attribute("y"), Integer?),
.value = CType(x, String),
.element = x
}).ToList()

For i = fonts.Count - 1 To 0 Step -1
If Not Integer.TryParse(fonts(i).value, value) Then
fonts(i).element.Remove()
End If
Next i

End Sub

End Module
Public Class Font
Public element As XElement
Public fill As String
Public fontFamily As String
Public size As Integer?
Public x As Integer?
Public y As Integer?
Public value As String


End Class






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 16:22









jdweng

16.6k2717




16.6k2717












  • OP is using VBA...
    – Tim Williams
    Nov 20 at 20:22










  • thanks but looking for a solution in VBA
    – subbu
    Nov 22 at 10:26


















  • OP is using VBA...
    – Tim Williams
    Nov 20 at 20:22










  • thanks but looking for a solution in VBA
    – subbu
    Nov 22 at 10:26
















OP is using VBA...
– Tim Williams
Nov 20 at 20:22




OP is using VBA...
– Tim Williams
Nov 20 at 20:22












thanks but looking for a solution in VBA
– subbu
Nov 22 at 10:26




thanks but looking for a solution in VBA
– subbu
Nov 22 at 10:26


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396408%2fremove-svg-xml-child-element-based-on-value%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Feedback on college project

Futebolista

Albești (Vaslui)